[英]call() is not working with getter and setter in javascript
let Name = { get fullname() { return this.fname + this.last } } function pension() { return this.age > 60 } let firstclass_employee = [{ fname: "sangeth", last: "AV", age: 21, address: { housename: "good house", place: "goodplace", city: "goodtwon", postcode: 121212 }, hobbies: ["driving", "travelling", "sports"] }, { fname: "ramu", last: "kv", age: 29, address: { housename: "etho veedu", place: "vayadi", city: "kalur", postcode: 11111 }, hobbies: ["travelling", "sports"] }] console.log(" objects::\t", Name, "\n", firstclass_employee) //calling a out side function for an object(out side function may be inside an object oe not) console.log("Fullname of first employee::", Name.fullname.call(firstclass_employee[0])) console.log("Pension status of of first emlpoyee::", pension.call(firstclass_employee[0])) console.log("Fullname of 2nd employee::", Name.fullname.call(firstclass_employee[1])) console.log("Pension status of of 2nd emlpoyee::", pension.call(firstclass_employee[1]))
AND getting error---> console.log("Fullname of first employee::", Name.fullname.call(firstclass_employee[0])) ^并出现错误---> console.log("第一个雇员的全名::", Name.fullname.call(firstclass_employee[0])) ^
TypeError: Name.fullname.call is not a function
TypeError: Name.fullname.call 不是 function
remove get
- which is not a valid here删除
get
- 这在这里无效
let Name = {
fullname() {
return this.fname + this.last
}
}
If you want regular function remove the get
and it will work fine.如果您想要常规的 function 删除
get
,它将正常工作。 Sorry, I was mistakenly said you have errors in the console.log()
part.抱歉,我误说你在
console.log()
部分有错误。
let Name = { fullname() { return this.fname + this.last } } function pension() { return this.age > 60 } let firstclass_employee = [{ fname: "sangeth", last: "AV", age: 21, address: { housename: "good house", place: "goodplace", city: "goodtwon", postcode: 121212 }, hobbies: ["driving", "travelling", "sports"] }, { fname: "ramu", last: "kv", age: 29, address: { housename: "etho veedu", place: "vayadi", city: "kalur", postcode: 11111 }, hobbies: ["travelling", "sports"] }] console.log(" objects::\t", Name, "\n", firstclass_employee) //calling a out side function for an object(out side function may be inside an object oe not) console.log("Fullname of first employee::", Name.fullname.call(firstclass_employee[0])) console.log("Pension status of of first emlpoyee::", pension.call(firstclass_employee[0])) console.log("Fullname of 2nd employee::", Name.fullname.call(firstclass_employee[1])) console.log("Pension status of of 2nd emlpoyee::", pension.call(firstclass_employee[1]))
As far as why the call()
is invalid is that Name.fullname
is not exposed as a function.至于为什么
call()
无效是Name.fullname
没有公开为 function。 It is exposed the same way as a field would be, eg它的暴露方式与字段相同,例如
const Name = {
fullname: 'Bob'
};
With that structure, you wouldn't expect to be able to call Name.fullname.call
, right?使用这种结构,您不会期望能够调用
Name.fullname.call
,对吗? But for the purposes of getting the value, that's what the prior code exposed to the outside world (the difference with the field being that you can set the value, whereas with a getter and no setter, the property is read only).但是为了获取值,这就是之前的代码暴露给外界的内容(与字段的区别在于您可以设置值,而使用 getter 而没有 setter,属性是只读的)。
The get
essentially tells the JavaScript engine that, whenever someone asks for the value of fullname
, give them the value of the fname
and last
properties concatenated together. get
本质上告诉 JavaScript 引擎,每当有人询问fullname
的值时,将fname
和last
属性连接在一起的值给他们。
Thus, when you ask for Name.fullname.call(firstclass_employee[0])
it first finds Name
.因此,当您询问
Name.fullname.call(firstclass_employee[0])
时,它首先找到Name
。 Then it asks for the value of fullname
.然后它询问
fullname
的值。 It gets back NaN
, because this
is the Name
object and it doesn't have fname
or last
properties, and it tries to coerce the first undefined
(from this.fname
) to a Number
for the addition operator, and gets NaN
.它返回
NaN
,因为this
是Name
object 并且它没有fname
或last
属性,并且它尝试将第一个undefined
(来自this.fname
)强制为加法运算符的Number
,并获取NaN
。 Then it tries to get the call
function on NaN
, and there is no such thing, so it throws an error.然后它试图在
NaN
上call
function ,并且没有这样的东西,所以它会抛出一个错误。
It appears as though you want to be able to get a full name from any object that has fname
and last
properties.似乎您希望能够从任何具有
fname
和last
属性的 object 中获取全名。 I would suggest using static method for that rather than an instance property getter.我建议为此使用 static 方法而不是实例属性获取器。
Similarly, a static method would work for determining employees who get a pension.同样,static 方法可用于确定领取养老金的员工。
Personally, I would just create a class
or series of class
es that I could compose together to bring this functionality together, rather than trying to use call
, but that's me.就个人而言,我只会创建一个
class
或一系列class
es,我可以将它们组合在一起以将这个功能组合在一起,而不是尝试使用call
,但这就是我。
const Name = { fullName(obj) { return `${obj.fname} ${obj.last}`; } }; const Pay = { getsPension(obj) { return obj.age > 60; } } let firstclass_employee = [{ fname: "sangeth", last: "AV", age: 21, address: { housename: "good house", place: "goodplace", city: "goodtwon", postcode: 121212 }, hobbies: ["driving", "travelling", "sports"] }, { fname: "ramu", last: "kv", age: 29, address: { housename: "etho veedu", place: "vayadi", city: "kalur", postcode: 11111 }, hobbies: ["travelling", "sports"] }] console.log('should be sangeth AV: ', Name.fullName(firstclass_employee[0])); console.log('should be false: ', Pay.getsPension(firstclass_employee[0]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.