Created
August 20, 2013 08:39
-
-
Save guo-yu/6278806 to your computer and use it in GitHub Desktop.
js prototype 继承实例中的参数问题
This file contains 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
var Book = function(name) { | |
this.name = name; | |
this.demo = '123'; | |
} | |
var Method = function() { | |
// 继承了 Book 的静态属性 this.demo = '123' | |
Book.apply(this, arguments); | |
} | |
Method.prototype.add = function() { | |
// 如何访问到 demo 这个实例中的东西呢? | |
console.log(this); | |
} | |
Book.prototype.fetch = new Method(); | |
var demo = new Book('123'); | |
// 保持这种API设计风格 | |
demo.fetch.add(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
另外一种方法
不要new 了,new个蛋啊。。。直接在function里把各种原型方法return回去。但大规模实例化的时候会造成内存浪费。。