【发布时间】:2014-12-04 18:57:49
【问题描述】:
我有两个构造函数,我只想在对象b 中使用对象a 方法而不使用new 关键字,因为我不想在不需要时创建具有所有属性的对象。例如,在构造函数 b 内部我想使用构造函数 b 方法。
function a(){
this.first=function(){
return 'aaaaaa'
}
this.last=function (){
return 'bbbbb'
}
}
function b(){
this.name= //call method last of constructor a.
}
var person= new b();
console.log(person.name) //bbbbb
注意:构造函数a包含两个方法。因此,在给定的示例中,我首先没有使用任何方法,但稍后可能需要。所以我不想创建new a()。我只想直接调用它
【问题讨论】:
-
你不能那样做。将方法移至原型。
标签: javascript