Javascript原型功能指南
我的代码如下:Javascript原型功能指南,javascript,node.js,Javascript,Node.js,我的代码如下: ***************** var Person = function (name, yearOfBirth, job) { this.name = name, this.yearOfBirth = yearOfBirth, this.job = job } Person.prototype.age = function () { return (2020 - this.yearOfBirth) } Person.prototype.fullName = funct
*****************
var Person = function (name, yearOfBirth, job) {
this.name = name,
this.yearOfBirth = yearOfBirth,
this.job = job
}
Person.prototype.age = function () {
return (2020 - this.yearOfBirth)
}
Person.prototype.fullName = function () {
return `${this.name} great`
}
Person.prototype.car = function () {
if (this.age < 18) {
return ('Dude u still need to get older to drive')
} else {
return ('You are eligible for a license')
}
}
Person.prototype.medals = "silver"
var john = new Person('John',2010,'Teacher')
console.log(john.name)
console.log(john.age())
console.log(john.fullName())
console.log(john.medals)
console.log(john.car())
Output:
John
10
John great
silver
You are eligible for a license
*****************
*****************
var Person=职能(姓名、出生年份、职务){
this.name=name,
this.yearOfBirth=出生年份,
这个工作
}
Person.prototype.age=函数(){
回归(2020年-今年出生)
}
Person.prototype.fullName=函数(){
返回`${this.name}太好了`
}
Person.prototype.car=函数(){
如果(此年龄<18岁){
return(‘老兄,你还需要变老才能开车’)
}否则{
return('您有资格获得许可证')
}
}
Person.prototype.carges=“银色”
var john=新人('john',2010,'Teacher')
console.log(john.name)
console.log(john.age())
console.log(john.fullName())
console.log(john.com)
console.log(john.car())
输出:
约翰
10
约翰很好
银
你有资格申请执照
*****************
现在,如果你能看到,虽然John Age只有10岁,理想的输出应该是“他需要变老”,但它只是返回else语句,或者我对原型的理解是错误的
多谢各位
如何在另一个函数中访问对象原型
谢谢你你制作了
age
a函数。你需要调用它:如果(this.age()<18){
该死,多了一双眼睛。我错过了添加函数,弄砸了脑袋。它按预期工作。谢谢。我还建议你使用类而不是函数。搞乱原型看起来更糟,更难理解。请检查