JavaScript 中对象的 constructor 属性的作用是什么?

来源:互联网 发布:且听风吟 朴树 知乎 编辑:程序博客网 时间:2024/05/17 17:44
作者:小鱼二
链接:https://www.zhihu.com/question/19951896/answer/67551712
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

var a,b;(function(){  function A (arg1,arg2) {    this.a = 1;    this.b=2;   }  A.prototype.log = function () {    console.log(this.a);  }  a = new A();  b = new A();})()a.log();// 1b.log();// 1

通过以上代码我们可以得到两个对象,a,b,他们同为类A的实例。因为A在闭包里,所以现在我们是不能直接访问A的,那如果我想给类A增加新方法怎么办?

// a.constructor.prototype 在chrome,firefox中可以通过 a.__proto__ 直接访问a.constructor.prototype.log2 = function () {  console.log(this.b)}a.log2();// 2b.log2();// 2

通过访问constructor就可以了。
或者我想知道a的构造函数有几个参数?

a.constructor.length

或者再复杂点,我想知道a的构造函数的参数名是什么(angular的依赖注入就是通过此方法实现的据说)

a.constructor .toString() .match(/\(.*\)/) .pop().slice(1,-1) .split(',');// ["arg1", "arg2"]
阅读全文
0 0
原创粉丝点击