Created
April 30, 2014 11:29
-
-
Save 1000copy/0da63e93ca0cec7dc0c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function extend(Child, Parent) { | |
| var F = function(){}; | |
| F.prototype = Parent.prototype; | |
| Child.prototype = new F(); | |
| Child.prototype.constructor = Child; | |
| Child.uber = Parent.prototype; | |
| } | |
| function Animal(cat){ | |
| this.species = "动物"; | |
| if (aa !==undefined) | |
| this.species = cat; | |
| } | |
| // function Cat(name,color){ | |
| // Animal.apply(this, arguments); | |
| // this.name = name; | |
| // this.color = color; | |
| // } | |
| function Cat(name,color,cat){ | |
| Animal.apply(this, [cat]); | |
| this.name = name; | |
| this.color = color; | |
| } | |
| extend(Cat,Animal); | |
| var cat1 = new Cat("大毛","黄色"); | |
| console.log(cat1.species); | |
| var cat1 = new Cat("大毛","黄色","wangwang"); | |
| console.log(cat1.species); | |
| // You can assign a different this object when calling an existing function. | |
| //this refers to the current object, the calling object. With apply, you can | |
| //write a method once and then inherit it in another object, without having | |
| //to rewrite the method for the new object. | |
| // 当调用 Animal.apply(this, [cat]); 发生了什么? | |
| // 执行Animal 方法,但是this换成了cat的this。这样,当执行Animal时,就会为This,也就是Cat加入一个属性,species = 动物。这个specics 写到了Cat内。多么神奇。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment