源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

深入分析js中的constructor和prototype

  • 时间:2022-04-21 10:27 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:深入分析js中的constructor和prototype
我们在定义函数的时候,函数定义的时候函数本身就会默认有一个prototype的属性,而我们如果用new 运算符来生成一个对象的时候就没有prototype属性。我们来看一个例子,来说明这个
[u]复制代码[/u] 代码如下:
function a(c){ this.b = c; this.d =function(){ alert(this.b); } } var obj = new a('test'); alert(typeof obj.prototype);//undefine alert(typeof a.prototype);//object
从上面的例子可以看出函数的prototype 属性又指向了一个对象,这个对象就是prototype对象,请看下图 [img]http://files.jb51.net/upload/201204/20120407105900500.png[/img] [b]a.prototype 包含了2个属性,一个是constructor ,另外一个是__proto__[/b] 这个constructor  就是我们的构造函数a,这个也很容易理解。 那么__proto__ 是什么呢? 这个就涉及到了原型链的概念:   每个对象都会在其内部初始化一个属性,就是__proto__,当我们访问一个对象的属性 时,如果这个对象内部不存在这个属性,那么他就会去__proto__里找这个属性,这个__proto__又会有自己的__proto__,于是就这样 一直找下去。 [b]请看mozzlia 对它对它的描述[/b] When an object is created, its [code]__proto__[/code] property is set to constructing function's [code]prototype[/code] property. For example [code]var fred = new Employee();[/code] will cause [code]fred.__proto__ = Employee.prototype;[/code]. This is used at runtime to look up properties which are not declared in the object directly. E.g. when [code]fred.doSomething()[/code] is executed and [code]fred[/code] does not contain a[code]doSomething[/code], [code]fred.__proto__[/code] is checked, which points to [code]Employee.prototype[/code], which contains a [code]doSomething[/code], i.e. [code]fred.__proto__.doSomething()[/code] is invoked. Note that [code][i]__proto__[/i][/code] is a property of the instances, whereas [code]prototype[/code] is a property of their constructor functions. 不管你信不信,我们来看图 [img]http://files.jb51.net/upload/201204/20120407105900987.png[/img] 在后面如果加上 alert(obj.__proto__ === a.prototype) //true 同理,在这里我们来分析出new 运算符做了那些事情 [list=1] [*] var obj={}; 也就是说,初始化一个对象obj。 [/*][*]obj.__proto__=a.prototype; [/*][*] a.call(obj);也就是说构造obj,也可以称之为初始化obj。 [/*][/list] 我们将这个例子改造一些,变得复杂一点。
[u]复制代码[/u] 代码如下:
function a(c){ this.b = c; this.d =function(){ alert(this.b); } } a.prototype.test = function(){ alert(this.b); } var obj = function (){} obj.prototype = new a('test'); obj.prototype.test1 =function(){ alert(22222); } var t = new obj('test'); t.test();//alert('test');
我们来分析下这个过程 由 var t = new obj('test'); 我们可以得到 t.__proto__ = obj.prototype,但是上面指定obj.prototype =new a('test'); 可以这样来看下 obj.prototype = p, p = new a('test'); p.__proto__ = a.prototype; 那么obj.prototype.__proto__ = a.prototype,由 t.__proto__ = obj.prototype 可以得出 t.__proto__.__proto__ = a.prototype, 所以对象t先去找本身是的prototype 是否有test函数,发现没有,结果再往上级找,即 t.__proto__ ,亦即obj.prototype 寻找test函数 ,但是obj.prototype 也没有这个函数,然后再往上找。即 t.__proto__.__proto__ 找,由于t.__proto__.__proto__ = a.prototype 在 a.prototype 中找到了这个方法,输出了alert('test') 从这里可以分析得出一个结论,js中原形链的本质在于 __proto__ 再看看一个列子
[u]复制代码[/u] 代码如下:
function a(c){ this.b = c; this.d =function(){ alert(this.b); } } var obj = new a('test'); alert(obj.constructor);//function a(){} alert(a.prototype.constructor);//function a(){}
根据上面讲到的__proto__ 我们来分析下,首先obj是没有constructor 这个属性的,但是 obj.__proto__ = a.prototype;就从 a.prototype中寻找,而 a.prototype.constructor 是就a,所有两者的结果是一一样的.
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部