[英]how to extend javascript classes
為什么子類沒有echo()方法?
Parent = function(){
this.name = 'abc';
}
Parent.prototype.echo = function(){
alert(this.name);
}
Child = function(){
$.extend(this, Parent);
}
var x = new Child();
x.echo();
我應該怎么做才能從Java的父類繼承?
您需要將Child
的原型設置為Parent
。
function Parent() {
this.name = 'abc';
}
Parent.prototype.echo = function () {
alert(this.name);
}
function Child() {
}
Child.prototype = new Parent()
var x = new Child();
x.echo();
我假設您使用jQuery。 jQuery擴展系統使用對象,而不是“類”,請參見: http : //api.jquery.com/jQuery.extend/
Child.prototype = new Parent()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.