{"action":"opened","issue":{"url":"https://api.github.com/repos/littlee/littlee.github.io/issues/2","labels_url":"https://api.github.com/repos/littlee/littlee.github.io/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/littlee/littlee.github.io/issues/2/comments","events_url":"https://api.github.com/repos/littlee/littlee.github.io/issues/2/events","html_url":"https://github.com/littlee/littlee.github.io/issues/2","id":102172815,"number":2,"title":"Inheritance","user":{"login":"littlee","id":7534668,"avatar_url":"https://avatars.githubusercontent.com/u/7534668?v=3","gravatar_id":"","url":"https://api.github.com/users/littlee","html_url":"https://github.com/littlee","followers_url":"https://api.github.com/users/littlee/followers","following_url":"https://api.github.com/users/littlee/following{/other_user}","gists_url":"https://api.github.com/users/littlee/gists{/gist_id}","starred_url":"https://api.github.com/users/littlee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/littlee/subscriptions","organizations_url":"https://api.github.com/users/littlee/orgs","repos_url":"https://api.github.com/users/littlee/repos","events_url":"https://api.github.com/users/littlee/events{/privacy}","received_events_url":"https://api.github.com/users/littlee/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-08-20T16:01:47Z","updated_at":"2015-08-20T16:01:47Z","closed_at":null,"body":"```js\r\n/*\r\n\t原型链继承\r\n\t给原型添加方法的代码一定要放在替换原型的语句之后\r\n\t不能使用对象字面量创建原型方法\r\n\t通过原型来实现继承，原型实际上会变成另一个类型的实例\r\n\t于是，原来的实例属性也变成了现在的原型属性\r\n\t创建子类型的实例时，不能向超类型的构造函数传递参数，\r\n\t实际上应该说是没有办法在不影响所有对象的实例的情况下，给超类型的构造函数传递参数\r\n*/\r\nfunction SuperType() {\r\n\tthis.property = true;\r\n}\r\n\r\nSuperType.prototype.getSuperValue = function() {\r\n\treturn this.property;\r\n};\r\n\r\nfunction SubType() {\r\n\tthis.subproperty = false;\r\n}\r\n\r\nSubType.prototype = new SuperType();\r\n\r\nSubType.prototype.getSubValue = function() {\r\n\treturn this.subproperty;\r\n};\r\n\r\nvar instance = new SubType();\r\nconsole.log(instance.getSuperValue()); // true;\r\n\r\n/*\r\n\t借用构造函数（伪造对象，经典继承）\r\n\t方法都在构造函数中定义，代码无法复用\r\n\t在超类型的原型中定义的方法，对子类型而言是不可见的\r\n\r\n*/\r\nfunction SuperType(name) {\r\n\tthis.name = name;\r\n\tthis.colors = ['red', 'green', 'blue'];\r\n}\r\n\r\nfunction SubType() {\r\n\tSuperType.call(this, 'My Name');\r\n\r\n\t// 实例属性\r\n\tthis.age = 22;\r\n}\r\n\r\nvar instance1 = new SubType();\r\ninstance1.colors.push('black'); // red, green, blue, black\r\nconsole.log(instance1.colors);\r\n\r\nvar instance2 = new SubType();\r\nconsole.log(instance2.colors); // red, green, blue\r\n\r\n/*\r\n\t组合继承（伪经典继承）\r\n\t最常用的继承方式，避免了原型链和借用构造函数的缺陷\r\n*/\r\nfunction SuperType(name) {\r\n\tthis.name = name;\r\n\tthis.colors = ['red', 'green', 'blue'];\r\n}\r\n\r\nSuperType.prototype.sayName = function() {\r\n\tconsole.log(this.name);\r\n};\r\n\r\nfunction SubType(name, age) {\r\n\tSuperType.call(this, name);\r\n\tthis.age = age;\r\n}\r\n\r\nSubType.prototype = new SuperType();\r\nSubType.prototype.constructor = SubType;\r\nSubType.prototype.sayAge = function() {\r\n\tconsole.log(this.age);\r\n}\r\n\r\nvar i1 = new SubType('Lee1', 22);\r\ni1.colors.push('black');\r\nconsole.log(i1.colors);\r\ni1.sayAge();\r\ni1.sayName();\r\n\r\nvar i2 = new SubType('Lee2', 33);\r\nconsole.log(i2.colors);\r\ni2.sayAge();\r\ni2.sayName();\r\n\r\n/*\r\n\t原型式继承\r\n\tES5 的 Object.create 方法规范化了这个方式\r\n*/\r\nfunction object(o) {\r\n\tfunction F() {}\r\n\tF.prototype = o;\r\n\treturn new F();\r\n}\r\n\r\n/*\r\n\t寄生式继承\r\n*/\r\nfunction createAnother(original) {\r\n\tvar clone = object(original);\r\n\tclone.sayHi = function() {\r\n\t\tconsole.log('HI');\r\n\t};\r\n\treturn clone;\r\n}\r\n\r\n/*\r\n\t寄生组合式继承\r\n*/\r\n```"}}