
var Person = function(name) { this.name = name }; var foo = new Person("foo"); foo // { name: 'foo' } foo.constructor.toString() // 'function (name) { this.name = name }' Person.prototype.constructor.toString() // 'function (name) { this.name = name }' // 改写 constructor 指向的构造函数 Person.prototype.constructor = function(name) { this.name = "bar" } Person.prototype.constructor.toString() // 'function (name) { this.name = "bar" }' var foo2 = new Person("foo2"); foo2.constructor.toString() // 'function (name) { this.name = "bar" }' // 期待是 { name: 'bar' } 因为构造函数已变了 // 但是... foo2 // { name: 'foo2' } 1 iwege 2016-01-13 22:18:13 +08:00 在最后试试 Person.toString() 看看。 Person 本来就是一个 function 了。 要改变的话直接赋值 Person = func 就好了。 |
2 tianzhen OP |
&nsp; 3 banricho 2016-01-14 00:58:56 +08:00 1. 函数都有 prototype 对象,其中默认包含一个属性 constructor ,一般情况下默认指向函数自身 2. 以你的例子来说, foo1. __proto__ === Person.prototype ,修改 constructor 是没有用的,它只是一个天生就默认存在的标志,但本身不具备实际意义 3. 你的任何实例化方法本身不存在 toString 方法,那么就会向构造函数 Person 上查找。没有的话就继续向 Function.prototype 上查找,还是没有就继续在 Function.prototype.__proto__ 也就是 Object.prototype 上查找,于是你里面所有的 toString 其实最终调用的都是 Object.prototype.toString 方法 4. 要达到你的效果修改 constructor 是没用的,关键是要在 foo1 和 foo2 之间切断原型链 |
4 banricho 2016-01-14 00:59:58 +08:00 @banricho “那么就会向构造函数 Person 上查找” => “那么就会向构造函数 Person.prototype 上查找” |
5 banricho 2016-01-14 01:12:32 +08:00 我搞错了,请看下面的 var Person = function(name) { this.name = name }; var foo = new Person("foo"); console.log(foo.toString === Person.prototype.toString); // true console.log(Person.prototype.toString === Person.prototype.__proto__.toString); // true console.log(Person.prototype.__proto__.toString === Object.prototype.toString); // true console.log(Person.toString === Function.prototype.toString); // true console.log(Function.prototype.toString === Object.toString); // true 我上面说的第 3 点有误。。。 |
6 banricho 2016-01-14 01:20:35 +08:00 还得补上这个 = =。 console.log(Person.toString === Function.prototype.toString); // true console.log(Function.prototype.toString === Object.toString); // true console.log(Function.prototype.toString === Function.toString); // true console.log(Function.toString === Object.toString); // true console.log(Function.__proto__ === Object.__proto__); // true 好微妙的关系(好了我去睡了,大晚上脑子混乱又乱扯了。。。) |
7 banricho 2016-01-14 04:02:49 +08:00 没睡着 =。= 爬起来**貌似**搞清了,直接上图 |
8 banricho 2016-01-14 04:08:44 +08:00 @banricho 真去睡了 大晚上脑子果然不清醒 Function.prototype.__proto__ 指向的应该是 Object.prototype 而不是 Person 的。。。 啊啊啊啊啊啊啊啊 救命啊好丢人 |
9 banricho 2016-01-14 04:25:45 +08:00 补完睡觉,刷屏抱歉 |
13 banricho 2016-01-14 10:29:50 +08:00 |
14 SilentDepth 2016-01-14 13:22:52 +08:00 @banricho 正纳闷这浓浓的 Paper 感是怎么揉进文字的,原来还有后期…… |
15 banricho 2016-01-14 15:26:13 +08:00 via Android |