原型链接的javascript继承不起作用
我遵循Stoyan Stefanov JavaScript模式的书,有一个卡住了的。。。我不能继承原型。为什么?我做错了什么?(在NodeJS上运行此代码)原型链接的javascript继承不起作用,javascript,node.js,prototype,Javascript,Node.js,Prototype,我遵循Stoyan Stefanov JavaScript模式的书,有一个卡住了的。。。我不能继承原型。为什么?我做错了什么?(在NodeJS上运行此代码) 箭头函数不打算用作方法Parent.prototype.say应该是正常功能。从 箭头函数表达式的语法比函数短 表达式和没有自己的此参数、super或 新目标。这些函数表达式最适合于非方法 函数,它们不能用作构造函数 //继承 函数父级(名称){ this.name=name | |“某个名字”; } Parent.prototype.s
箭头函数不打算用作方法
Parent.prototype.say
应该是正常功能。从
箭头函数表达式的语法比函数短
表达式和没有自己的此
参数、super或
新目标。这些函数表达式最适合于非方法
函数,它们不能用作构造函数
//继承
函数父级(名称){
this.name=name | |“某个名字”;
}
Parent.prototype.say=function(){//普通函数
返回此.name;
};
函数子项(名称){
//this.name='123';
}
函数继承(C,P){
C.原型=新的P();
}
继承(子女、父母);
console.log(Child.prototype);
const kid=新的子对象();
控制台日志(kid);
console.log(kid.name);
console.log(kid.say())代码>箭头函数不打算用作方法Parent.prototype.say
应该是正常功能。从
箭头函数表达式的语法比函数短
表达式和没有自己的此
参数、super或
新目标。这些函数表达式最适合于非方法
函数,它们不能用作构造函数
//继承
函数父级(名称){
this.name=name | |“某个名字”;
}
Parent.prototype.say=function(){//普通函数
返回此.name;
};
函数子项(名称){
//this.name='123';
}
函数继承(C,P){
C.原型=新的P();
}
继承(子女、父母);
console.log(Child.prototype);
const kid=新的子对象();
控制台日志(kid);
console.log(kid.name);
console.log(kid.say())代码>箭头函数不打算用作方法Parent.prototype.say
应该是普通函数。()=>this.name代码>正在使用声明上下文而不是父级上下文。箭头函数不打算用作方法Parent.prototype.say
应该是普通函数。()=>this.name
正在使用声明上下文而不是父上下文。Eslint说不要使用未命名函数。这只是因为AirBnb linting语法的缘故?你可以给它加个名字。:)<代码>函数say(){
和Eslint说不要使用未命名的函数。这只是因为AirBnb-linting语法?你可以给它添加一个名称。:函数say(){
// inheritance
function Parent(name) {
this.name = name || 'some Name';
}
Parent.prototype.say = () => this.name;
function Child(name) {
// this.name = '123';
}
function inherit(C, P) {
C.prototype = new P();
}
inherit(Child, Parent);
debug(Child.prototype);
const kid = new Child();
debug(kid);
debug(kid.name);
debug(kid.say());