javascript继承报告没有乐趣
我定义了一个javascript继承函数并尝试继承一个类javascript继承报告没有乐趣,javascript,Javascript,我定义了一个javascript继承函数并尝试继承一个类 function extends(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; } function MyCl
function extends(Child, Parent) {
var F = function(){};
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.uber = Parent.prototype;
}
function MyClass (){
};
MyClass.prototype.doSomeThing=function(){
};
然后我写了
var a={};
extends( a , MyClass );
a.doSomeThing();
但是它报告说,doSomeThing不是一个函数
欢迎发表评论您的变量A是一个对象,而不是构造函数/函数,因此A.doSomething不是函数,而是A.prototype.doSomething
将a转换为函数或释放子对象。原型=。。要让它工作。
一个
也必须是一个构造函数,而不是一个简单的对象。查看extend
的实现,您有Child.prototype=new F()代码>在那里。JavaScript没有类。