1、javascript中的每个引用类型(原生的、和自定义的)都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用。

A.prototype = new B();

理解prototype不应把它和继承混淆。A的prototype为B的一个实例,可以理解A将B中的方法和属性全部克隆了一遍。A能使用B的方 法和属性。这里强调的是克隆而不是继承。可以出现这种情况:A的prototype是B的实例,同时B的prototype也是A的实例。

     if (true) {
function baseClass()
{
this.showMsg = function()
{
alert("baseClass::showMsg");
}
} function extendClass()
{
this.hello= function(){alert("子类");}
} extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg();
baseClass.prototype=new extendClass();
var base=new baseClass();
base.hello(); 2    if (true) {
            function baseClass()
        {
          this.showMsg = function()
          {
             alert("baseClass::showMsg");   
          }
        }         function extendClass()
        {
            this.hello= function(){alert("子类");}
        }         extendClass.prototype = new baseClass();
        var instance = new extendClass();
        instance.showMsg();
        baseClass.prototype=new extendClass();
        var base=new baseClass();
        base.hello();     }    if (true) {
            function baseClass()
        {
          this.showMsg = function()
          {
             alert("baseClass::showMsg");   
          }
        }         function extendClass()
        {
            this.hello= function(){alert("子类");}
        }         extendClass.prototype = new baseClass();
        var instance = new extendClass();
        instance.showMsg();
        baseClass.prototype=new extendClass();
        var base=new baseClass();
        base.hello();     }    if (true) {
            function baseClass()
        {
          this.showMsg = function()
          {
             alert("baseClass::showMsg");   
          }
        }         function extendClass()
        {
            this.hello= function(){alert("子类");}
        }         extendClass.prototype = new baseClass();
        var instance = new extendClass();
        instance.showMsg();
        baseClass.prototype=new extendClass();
        var base=new baseClass();
        base.hello();     }

if (true) {
            function baseClass()
        {
          this.showMsg = function()
          {
             alert("baseClass::showMsg");   
          }
        }

function extendClass()
        {
            this.hello= function(){alert("子类");}
        }

extendClass.prototype = new baseClass();
        var instance = new extendClass();
        instance.showMsg();
        baseClass.prototype=new extendClass();
        var base=new baseClass();
        base.hello();

}

2、原型寻找阻断:即先从自己找方法和属性 ,找不到才去原型中去找;找到了就阻断询找:

function baseClass()
{
this.showMsg = function()
{
alert("baseClass::showMsg");
}
} function extendClass()
{
this.showMsg =function ()
{
alert("extendClass::showMsg");
}
} extendClass.prototype = new baseClass();
var instance = new extendClass(); instance.showMsg();//显示extendClass::showMsg

实验证明:函数运行时会先去本体的函数中去找,如果找到则运行,找不到则去prototype中寻找函数。或者可以理解为prototype不会克隆同名函数。

3、

如果我想使用extendClass的一个实例instance调用baseClass的对象方法showMsg怎么办?

答案是可以使用call:

extendClass.prototype = new baseClass();
var instance = new extendClass(); var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//显示baseClass::showMsg

4、在写复杂的 JavaScript 应用之前,充分理解原型链继承的工作方式是每个 JavaScript 程序员必修的功课。 要提防原型链过长带来的性能问题,并知道如何通过缩短原型链来提高性能。 更进一步,绝对不要扩展内置类型的原型,除非是为了和新的 JavaScript 引擎兼容。

5、hasOwnProperty 函数

// 修改Object.prototype
Object.prototype.bar = 1;
var foo = {goo: undefined}; foo.bar; // 1
'bar' in foo; // true foo.hasOwnProperty('bar'); // false
foo.hasOwnProperty('goo'); // true

JavaScript 不会保护 hasOwnProperty 被非法占用,因此如果一个对象碰巧存在这个属性, 就需要使用外部hasOwnProperty 函数来获取正确的结果。

var foo = {
hasOwnProperty: function() {
return false;
},
bar: 'Here be dragons'
}; foo.hasOwnProperty('bar'); // 总是返回 false // 使用其它对象的 hasOwnProperty,并将其上下为设置为foo
{}.hasOwnProperty.call(foo, 'bar'); // true

当检查对象上某个属性是否存在时,hasOwnProperty唯一可用的方法。 同时在使用 for in loop 遍历对象时,推荐总是使用 hasOwnProperty 方法, 这将会避免原型对象扩展带来的干扰。

for in 循环

in 操作符一样,for in 循环同样在查找对象属性时遍历原型链上的所有属性。推荐总是使用 hasOwnProperty。不要对代码运行的环境做任何假设,不要假设原生对象是否已经被扩展了

js学习之原型prototype(一)的更多相关文章

  1. js中的原型prototype

    var arr1 = new Array(12,34,98,43,38,79,56,1); arr1.sum=function (){ var result = 0; for(var i=0; i&l ...

  2. JS 中的原型 -- prototype、__proto__ 以及原型链

    原文: 1.深入理解javascript原型和闭包——prototype原型 2.三张图搞懂JavaScript的原型对象与原型链 打开浏览器控制台,任意定义一个对象,打印出来后,会发现有最后一定有一 ...

  3. [前端JS学习笔记]JavaScript prototype 对象

    一.概念介绍 prototype 对象 : 原型对象.在JavaScript中, 每一个对象都继承了另一个对象,后者称为"原型对象". 只有 null 除外,它没有自己的原型对象. ...

  4. [js学习笔记] 原型链理解

    js中只有对象,包括对象,函数,常量等. prototype 只有函数里有这个属性. 对象里可以设置这个属性,但是没这个属性的意义 prototype指向一个对象,可以添加想要的方法. 该对象里有一个 ...

  5. js 中的原型prototype

    每次创建新函数,就会根据规则为该函数创建一个 prototype 属性,该属性是一个指向函数原型对象的指针.并且原型对象都默认拥有一个 constructor 属性,该属性是一个指向那个新建函数的指针 ...

  6. JS原型,Prototype,原型

    对于javascript这样一种前端语言,个人觉得,要真正的理解其oop, 就必须要彻底搞清楚javascript的对象,原型链,作用域,闭包,以及this所引用的对象等概念.这些对弄明白了,应该就可 ...

  7. js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

    js最好的继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } Class ...

  8. js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法。

    js一种继承机制:用对象冒充继承构造函数的属性,用原型prototype继承对象的方法. function ClassA(sColor) { this.color = sColor; } ClassA ...

  9. 关于JS对象原型prototype与继承,ES6的class和extends · kesheng's personal blog

    传统方式:通过function关键字来定义一个对象类型 1234567891011 function People(name) { this.name = name}People.prototype. ...

随机推荐

  1. 瑞昱Realtek(Realtek HD Audio Driver)音频声卡驱动R2.49 for Win7_Vista

    不管是在高端系列主板上,还是在低端系列主板上,我们都能看到Realtek瑞昱的身影,Realtek HD Audio Driver能够支持所有的Realtek HD Audio音频驱动.Realtek ...

  2. mysql function 与 procedure

    Mysql 的 function 和 procedure 有啥区别呢 ? 网上搜索后说 function 有返回值, procedure 无返回值. 1.return  从function 的语法角度 ...

  3. CSS3制作时钟

    这个效果是一个CSS3制作的时钟,不过并不是我们传统的时钟风格,分别用三块显示 时.分.秒三个部分,而且这个DEMO中藤藤还为其加上了一个js的效果,能让这个效 果和现实时钟的时间同步.这个效果运用到 ...

  4. AForm — 模型驱动的自动化表单解决方案

    http://xiehuiqi220.github.io/AForm/doc/book/#

  5. 四大主流云平台对比--CloudStack, Eucalyptus, vCloud Director和OpenStack。

    我迟早可能都要进入的领域,提前温习... 还有KVM,ESXI,API,XEN之间的术语和关系,也要心中有数.. ~~~~~~~~~~~~~~~~~~~ 云计算在如今的IT界一直是一个最热门的话题,鉴 ...

  6. 服务器部署_nginx的host not found in upstream "tomcat_www.bojinne" 错误解决办法

    今天修改了nginx.conf之后,nginx-t报错. 1. 网上多认为此错误需要修改/etc/hosts,添加该域名对应的ip 2. 我自己的解决方案是仔细核对 upstream 后面的字符 和  ...

  7. loadrunner_Controller技巧_overlay

    在scenario运行期间,我们经常有类似于:总结Vu数变化,Tps 或者response time变化的趋势或者对比response time 和 tps,那么我们就用的到 Controller的图 ...

  8. DHTMLX 前端框架 建立你的一个应用程序 教程(九)--绑定表单Form到表格Grrid中

    绑定表单Form到表格Grrid中 现在我们需要选中一行表格数据的时候 数据能在表单中显示出来 我们可以使用DHTMLX 丰富的组件功能实现它. 绑定表单到表格 1.调用bind方法将表单绑定到网格, ...

  9. WPF中的MatrixTransform

    原文:WPF中的MatrixTransform WPF中的MatrixTransform                                                         ...

  10. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...