Skip to content

Instantly share code, notes, and snippets.

@mactive
Created December 26, 2016 06:25
Show Gist options
  • Save mactive/b088ac924ec1661600a28675d89e6242 to your computer and use it in GitHub Desktop.
Save mactive/b088ac924ec1661600a28675d89e6242 to your computer and use it in GitHub Desktop.
prototype chain
function Animal(age) {
if (age) { this.age = age; }
// 这个name 不会影响 Dog 或者V, 只会影响new Animal的人
this.name= "One Animal"
}
Animal.prototype.age = 1;
Animal.prototype.name= "One Animal"; // 优先级 P3
function Dog(name) {
if (name) { this.name = name; }
}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.name = "Bubbles"; // 优先级 P2
var v = new Dog('Vincent'); // Vincent 优先级 P1
console.log(v.name); // Vincent
console.log(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment