Why the second pattern is used more then the 1st when both of them do the same? Or I'm wrong?
function Foo1(){
this.name = "Foo 1";
this.hello = function(){
console.log("hello", this.name);
};
}
var test1 = new Foo1();
test1.hello();
function Foo2(){
this.name = "Foo 2";
}
Foo2.prototype.hello = function(){
console.log("hello", this.name);
};
var test2 = new Foo2();
test2.hello();
Objects occupy less memory this way. There needs to be only one function in a class, as opposed to one function for each instance. This also mirrors classical inheritance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.