<!DOCTYPE html> <html> <head> <title></title> </head> <body> </body> <script> var People = function(){ this.name = "Jackey"; }; People.prototype.getName = function(){ return this.name; }; var Man = function(){ this.sex = "male"; People.call(this); }; //缺點 ,name 是父類中的屬性,若是修改會影響子類 //Man.prototype = People.prototype; //new 新開內存 Man.prototype = new People(); Man.prototype.constructor = Man;//可隨意修改People的屬性,不會影響到Man var man = new Man(); console.log(man.getName()); </script> </html>
<!DOCTYPE html> <html> <head> <title></title> </head> <body> </body> <script> var People = function(){ this.name = "Jackey"; }; People.prototype.getName = function(){ return this.name; }; var Man = function(){ this.sex = "male"; People.call(this); }; //缺點 ,name 是父類中的屬性,若是修改會影響子類 //Man.prototype = People.prototype; //new 新開內存 Man.prototype = new People(); Man.prototype.constructor = Man;//可隨意修改People的屬性,不會影響到Man var man = new Man(); console.log(man.getName()); </script> </html>
<!DOCTYPE html> <html> <head> <title></title> </head> <body> </body> <script> var People = function(){ this.name = "Jackey"; }; People.prototype.getName = function(){ return this.name; }; var Man = function(){ this.sex = "male"; People.call(this); }; //缺點 ,name 是父類中的屬性,若是修改會影響子類 //Man.prototype = People.prototype; //new 新開內存 Man.prototype = new People(); Man.prototype.constructor = Man;//可隨意修改People的屬性,不會影響到Man var man = new Man(); console.log(man.getName()); </script> </html>