函数混淆的代价者
costructor of function confusion
在这种情况下为
var A = function(){
this.d = 123;
}
A.prototype.c = 999;
A.prototype.d = 333;
var B = (new A()).constructor;
console.log(""+B);
console.log(new A().d); // 123
console.log(new B().d); // 123
console.log(new A().c); // 999
console.log(Object.getPrototypeOf(new B()).c); // 999 why?
A和B共享同一构造函数但B不是A,为什么有和A相同的原型?
在这种情况下,
var A = function(){
this.d = 123;
}
A.prototype.c = 999;
A.prototype.d = 333;
var B = A.constructor;
console.log(""+B);
console.log(new A().d); // 123
console.log(new B().d); // undefined
console.log(B.d); // still undefined
console.log(new A().c); // 999
console.log(Object.getPrototypeOf(new B()).c); // undefined
B是A的构造函数,而不是他的实例的构造函数
什么是B?如何在没有A实例的情况下访问A的构造函数?
调用new A()
时,将创建一个新的A
对象,其原型为A.prototype
。当您请求(new A()).constructor
时,您正在从该A
实例的原型链访问constructor
属性;这将是CCD_ 7。
A
本身就是一个Function
对象。也就是说:A
是Function
的一个实例。当您请求A.constructor
时,您正在从该Function
实例的原型链访问constructor
属性;这将是CCD_ 15。
在第一种情况下,B
和A
是对完全相同函数的引用。完全可以预期new A()
和new B()
的结果将具有相同的性质和相同的原型链。
在第二个示例中,B
是Function
构造函数,即构造函数的函数。调用new B()
会创建一个新的Function
对象。因此,new B()
的结果不具有与A
实例相同的属性。
要区分A
和new A()
:
c = new A(); // This is an instance of A. The constructor property is A.
console.log(c.constructor) // function() { this.d = 123; }
console.log(new c.constructor()) // Creates another instance of A.
console.log(Object.getPrototypeOf(new c.constructor())) // A {c: 999, d: 333}
var c = A; // This is a function. The constructor property is a base Function.
console.log(c.constructor) // function Function() { [native code] }
console.log(new c.constructor()) // Creates instance of base Function.
console.log(Object.getPrototypeOf(new c.constructor())) // function Empty() {}
如果自定义构造函数(A
)上没有new
运算符,则不会创建A
的实例。
有关new
操作员的详细信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new
尽管它已经得到了回答,但我没有看到它明确表示B是A(哦,它最近错过的APSiller提到了它):
var A = function(){};
var B = (new A()).constructor;
console.log(B===A,B===A.prototype.constructor
,A.prototype.constructor ===A);//true true true
//Something.prototype.constructor is a circular reference as constructor
//references Something
console.log(A.prototype.constructor.prototype.constructor.prototype
.constructor.prototype.constructor === A);//true
构造函数附带原型并设置为构造函数函数,但您可以覆盖它(通常在继承时完成,因此您通常会看到它在继承后得到修复)
Child.prototype=Parent.prototype;
//Child.prototype.constructor now incorrectly refers to Parent
console.log(Child.prototype.constructor===Parent);//true
//repair constructor
Child.prototype.constructor=Child;
关于继承、构造函数和原型的更多信息,请点击此处。
由于所有对象都有一个原型(除非使用Object.create(null)创建),所有对象都具有指向创建它们的函数的构造函数属性:
console.log([].constructor===Array);
var arr = new [].constructor(1,2,3);//same as new Array(1,2,3)
console.log(arr);//[1,2,3]
//the following temporarily casts the string to String
console.log("hello".constructor===String);//true
//same with numbers
console.log(1..constructor===Number);//true
console.log(function(){}.constructor === Function);//true
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- 比较从函数和生成的日期对象
- jQuery中是否内置了任何字符串格式化函数
- 函数混淆的代价者