JavaScript中的原型继承

JavaScript中的原型继承,javascript,Javascript,我不熟悉javascript的原型继承和js面向对象编程。我试图创建一个基本对象Account,然后从中继承CheckingAccount。下面是我的代码 function Account(fName, lName) { this.firstName = fName; this.lastName = lName; } Account.prototype.balance = function() { console.log("This is the balance");

我不熟悉javascript的原型继承和js面向对象编程。我试图创建一个基本对象Account,然后从中继承CheckingAccount。下面是我的代码

function Account(fName, lName) {
   this.firstName = fName;
   this.lastName = lName;

 }

 Account.prototype.balance = function() {
    console.log("This is the balance");
 }

 function CheckingAccount() {

    this.salary = 10000;
 }

 CheckingAccount.prototype = Object.create(Account.prototype);

 let account = new Account("John", "Doe");
 let checking = new CheckingAccount();
 CheckingAccount.balance();

当我在Visual Studio上运行它时,我得到以下错误:“uncaughttypeerror:CheckingAccount.balance不是一个函数”

您希望对实例而不是类对象调用该方法(例如,
checking
not
CheckingAccount
)。另外,请确保更改构造函数。如需进一步阅读,请参阅

您可以在此处看到这些更改:

功能帐户(fName,lName){
this.firstName=fName;
this.lastName=lName;
此值为0.salary=0;
}
Account.prototype.balance=函数(){
返回this.salary;//此处可能不使用_only_usalary
}
函数CheckingAccount(){
这个工资=10000;
}
CheckingAccount.prototype=Object.create(Account.prototype);
//确保更新构造函数
Object.defineProperty(CheckingAccount.prototype,“构造函数”{
值:CheckingAccount,
enumerable:false,//因此它不会出现在“for-in”循环中
可写:对
});
let account=新账户(“John”、“Doe”);
let checking=new CheckingAccount();
console.log('account balance:',account.balance())
console.log('checking balance:',checking.balance())
console.log('account-constructor:',account.constructor');

log('checking constructor:',checking.constructor')
您可以使用ES6中适当的
关键字来执行此操作。下面是Account和Chequing的一个示例,它扩展了Account和Chequing。这里,
账户
类具有
余额
存款
展示本
功能,只有子
支票
类具有
写支票
功能。然而,
Chequing
实例仍然可以调用所有父类函数,因为它是一个
帐户

类帐户{
建造师(姓名){
this.name=名称;
这个数字等于0;
}
余额(){
退还这个金额
}
押金(总额){
该金额+=总和;
}
展示册(){
console.log(“---------------------------”);
console.log(“Account for”,this.name);
log(`Balance:$${this.amount}`);
}
}
类Chequing扩展帐户{
建造师(姓名){
超级(姓名);
这是。支票=[];
}
书面请求(总和,至){
本金额-=总和;
this.checks.push({编号:this.checks.length+1,收件人:to,金额:sum});
}
展示册(){
super.showBook();
如果(this.cheques.length>0){
控制台日志(“支票活动:”);
(本支票的施工支票){
log(`${check.number}:$${check.amount}支付给${check.recipient}`);
}
}
else console.log(“未开具支票”);
}
}
const ch1=新的Chequing(“玛丽·琼斯”);
ch1.存款(1000);
ch1.showBook();
ch1.书面质询(95,“鲍勃·史密斯”);
ch1.writeCheque(485,“丽思酒店”);

ch1.showBook()账户在这里上课。 CheckingAccount也是一个类。 帐户检查都是实例

如果希望CheckingAccountAccount类继承,则应执行以下操作:

CheckingAccount.prototype = Object.create(Account.prototype);
这会将CheckingAccount的原型对象委托给Accounts原型对象

CheckingAccount函数中没有balance属性(函数是javascript中的一级对象),只有prototype属性。尽管同时是函数的余额属性驻留在对象上,Account的prototype属性指向该对象(简称balance函数位于Account.prototype对象的内部)

可从以下位置访问函数
balance()

  • 账户原型
  • CheckingAccount.prototype(因为您使用了Object.create函数 (已使用)
  • 从这两个类的任何实例

您必须使用实例:
checking.balance()
此外,您的
CheckingAccount
应该调用超级构造函数,并传递名字和姓氏。
新帐户(“John”、“Doe”)
是一个单独的对象,您将无法访问
检查对象上的名称。谢谢。我有个问题。对于每个继承的属性,我应该包含类似于以下内容的代码:Account.prototype.balance=function(){return this.salary;}@Massey所有这些都是将原型函数扩展到另一个“类”。您正通过此分配显式强制执行继承。在
balance()
的情况下,它是超类上的一个方法,因此如果您希望它在每个子类中都可用,您应该定义它。不过,您不需要定义它。没有严格的规定。您还可以覆盖它——在您的示例中,使用
CheckingAccount.prototype.balance=function(){/*something*/}。这完全取决于你想做什么。