Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript对象原型和Object.create方法_Javascript_Oop_Prototype - Fatal编程技术网

Javascript对象原型和Object.create方法

Javascript对象原型和Object.create方法,javascript,oop,prototype,Javascript,Oop,Prototype,Person.prototype和Object.createPerson.prototype之间有什么区别?我可以用每一个吗 function Person(name) { this.name = name; } Person.prototype.copy = function() { return new this.constructor(this.name); }; // define the Student class function Student(

Person.prototype和Object.createPerson.prototype之间有什么区别?我可以用每一个吗

function Person(name) {
    this.name = name;
}  

Person.prototype.copy = function() {  
    return new this.constructor(this.name);
};  

// define the Student class  
function Student(name) {  
    Person.call(this, name);
}  

// inherit Person  
Student.prototype = Person.prototype;
//Student.prototype = Object.create(Person.prototype);
最好使用Student.prototype=Object.createPerson.prototype而不是Student.prototype=Person.prototype

原因是在后一种情况下,两个原型共享一个共同的对象。所以我们在Student prototype中添加了一个新方法,person prototype也将访问该属性。例如:-

Student.prototype = Person.prototype
Student.prototype.test = function(){ alert('in student');};
var person = new Person();
person.test();
这将提醒“in student”

最好使用student.prototype=Object.createPerson.prototype而不是student.prototype=Person.prototype

原因是在后一种情况下,两个原型共享一个共同的对象。所以我们在Student prototype中添加了一个新方法,person prototype也将访问该属性。例如:-

Student.prototype = Person.prototype
Student.prototype.test = function(){ alert('in student');};
var person = new Person();
person.test();

这将向“in student”发出警报

另请参见:另请参见:使用您的代码和student.prototype=Object.createPerson.prototype向“in student”发出警报。因此,它们共享相同的引用。那么,有什么区别呢?如果你得到了警告函数Student{}function Person{}Student.prototype=Object.createPerson.prototype,你能试试这个吗;Student.prototype.test=函数{alert'in Student';};var人员=新人员;个人测试@Olegalex:这是不可能的,有对象和没有对象都不能得到相同的结果。create.@WiktorZychla我在Firefox中得到了相同的结果。这两个变量都会提醒学生:`function Personname{this.name=name;}Person.prototype.copy=function{return new this.constructorhis.name;};//定义学生类函数Studentname{Person.callthis,name;}//inherit Person Student.prototype=Person.prototype//Student.prototype=Object.createPerson.prototype;Student.prototype=Person.prototype Student.prototype.test=函数{alert'in Student';};var人员=新人员;个人测试`@你不是。用你的代码和Student.prototype=Object.createPerson.prototype向我检查这把小提琴,它也会向“in Student”发出警报。因此,它们共享相同的引用。那么,有什么区别呢?如果你得到了警告函数Student{}function Person{}Student.prototype=Object.createPerson.prototype,你能试试这个吗;Student.prototype.test=函数{alert'in Student';};var人员=新人员;个人测试@Olegalex:这是不可能的,有对象和没有对象都不能得到相同的结果。create.@WiktorZychla我在Firefox中得到了相同的结果。这两个变量都会提醒学生:`function Personname{this.name=name;}Person.prototype.copy=function{return new this.constructorhis.name;};//定义学生类函数Studentname{Person.callthis,name;}//inherit Person Student.prototype=Person.prototype//Student.prototype=Object.createPerson.prototype;Student.prototype=Person.prototype Student.prototype.test=函数{alert'in Student';};var人员=新人员;个人测试`@你不是。检查这把小提琴