【问题标题】:call() is not working with getter and setter in javascriptcall() 不适用于 javascript 中的 getter 和 setter
【发布时间】:2020-05-29 14:32:49
【问题描述】:

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]))

并且出现错误---> console.log("第一个员工的全名::", Name.fullname.call(firstclass_employee[0])) ^

TypeError: Name.fullname.call 不是函数

【问题讨论】:

  • 你想创建一个吸气剂吗?如果是这样,您使用Name.fullname 得到的是函数返回的内容,而不是函数本身。如果你想要一个常规函数,你不需要一个 getter。

标签: javascript function api object


【解决方案1】:

删除get - 这里无效

let Name = {
   fullname() {
        return this.fname + this.last
    }
}

【讨论】:

  • get 在那里有效;它创建了一个属性获取器。无效的是在属性 getter 上使用 call
  • 这里为什么 call() 无效,
【解决方案2】:

如果您想要常规功能,请删除get,它会正常工作。抱歉,我误认为您在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]))

【讨论】:

  • console.log 接受“无限”个参数并回显每个参数,并用空格分隔,所以逗号很好。
  • 对不起,这是我的错。
  • console.log() 在这个意义上被广泛滥用。当您使用 + 将对象转换为字符串时,您会丢失有价值的信息。
  • @ÁlvaroGonzález @HerecticMonkey 感谢您让我知道有关console.log()的一些有价值的信息
【解决方案3】:

至于call() 无效的原因是Name.fullname 没有作为函数公开。它的暴露方式与字段相同,例如

const Name = {
  fullname: 'Bob'
};

使用这种结构,您不会期望能够调用Name.fullname.call,对吧?但是为了获取值,这就是之前的代码暴露给外界的内容(与字段的区别在于您可以设置值,而使用getter而没有setter,该属性是只读的)。

get 实质上是告诉 JavaScript 引擎,每当有人请求 fullname 的值时,将fnamelast 属性串联在一起的值提供给他们。

因此,当您请求 Name.fullname.call(firstclass_employee[0]) 时,它首先会找到 Name。然后它询问fullname 的值。它返回NaN,因为thisName 对象并且它没有fnamelast 属性,它试图将第一个undefined(来自this.fname)强制转换为Number 用于加法运算符,并得到 NaN。然后尝试获取NaN上的call函数,没有这个东西,所以抛出了错误。


您似乎希望能够从任何具有fnamelast 属性的对象中获取全名。我建议为此使用静态方法而不是实例属性获取器。

同样,静态方法也适用于确定领取养老金的员工。

就我个人而言,我只会创建一个class 或一系列classes,我可以将它们组合在一起以将这个功能结合在一起,而不是尝试使用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]));

【讨论】:

  • 我已经在没有使用 getter 的情况下做到了。但我需要知道为什么 call() 在我的使用 getter 的代码中无效
猜你喜欢
  • 1970-01-01
  • 2022-06-10
  • 2017-06-24
  • 2012-04-14
  • 2021-01-05
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多