84669 personnes étudient
152542 personnes étudient
20005 personnes étudient
5487 personnes étudient
7821 personnes étudient
359900 personnes étudient
3350 personnes étudient
180660 personnes étudient
48569 personnes étudient
18603 personnes étudient
40936 personnes étudient
1549 personnes étudient
1183 personnes étudient
32909 personnes étudient
var a=new Object(){ a.x=1; a.prototype.y=2;} var b = Object.create(a) 假如这样的话。B的原型指向a,a的原型里面有x这个值,那么a的x继承给了b很正常,问题来了,b有没有继承到y这个值?
Questions sur les questions et réponses sur le site Web chinois prototype-PHP-Questions sur les questions et réponses sur le site Web chinois prototype-PHP
Jetez un œil et apprenez.
第一、prototype是一个构造函数的属性,不是一个对象实例的属性,所以,你这a.prototype.y=2;这句话是不对的,因为实例a没有prototype属性,按你的目的,应该这样写:Object.prototype.y=2;这样的话,a的原型中就有y属性了,且值为2。又因为a是b的原型,所以b从原型链上继承了y属性,即b.y的值为2。
代码
<!DOCTYPE html> <html> <head> <title>this is a title</title> </head> <body> <script> var a =new Object(); a.x=1; Object.prototype.y=2 //写成 a.prototype.y=2是不对的!!! var b= Object.create(a); console.log(a.y); //2 console.log(b.y); //2 </script> </body> </html>
Questions sur les questions et réponses sur le site Web chinois prototype-PHP-Questions sur les questions et réponses sur le site Web chinois prototype-PHP
Jetez un œil et apprenez.
第一、prototype是一个构造函数的属性,不是一个对象实例的属性,所以,你这a.prototype.y=2;这句话是不对的,因为实例a没有prototype属性,按你的目的,应该这样写:Object.prototype.y=2;这样的话,a的原型中就有y属性了,且值为2。又因为a是b的原型,所以b从原型链上继承了y属性,即b.y的值为2。
代码