Skip to content

Instantly share code, notes, and snippets.

@guo-yu
Created August 20, 2013 08:39
Show Gist options
  • Save guo-yu/6278806 to your computer and use it in GitHub Desktop.
Save guo-yu/6278806 to your computer and use it in GitHub Desktop.
js prototype 继承实例中的参数问题
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();
@guo-yu
Copy link
Author

guo-yu commented Aug 20, 2013

另外一种方法

不要new 了,new个蛋啊。。。直接在function里把各种原型方法return回去。但大规模实例化的时候会造成内存浪费。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment