本文介绍了Uncaught SyntaxError:Setter必须只有一个形式参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试理解JS上的getter和setter,我似乎无法传递此错误。任何人都可以提供任何有关我出错的原因吗?
I'm trying to understand getters and setters on JS and I can't seem to get pass this error. Can anyone provide any insight as to why I'm getting this error?
var book = {
year: 2004,
edition:1,
get newYear(){
return "Hello, it's " + this.year;
},
set newYear(y, e){
this.year = y;
this.edition = e;
}
};
推荐答案
当您指定setter表示的值时,将调用setter函数:
The setter function is called when you assign the value that setter represent:
var obj = {
set a(newVal) { console.log("hello"); }
}
obj.a = 1; // will console log "hello"
正如你所看到的那样对于一个二传手机没有意义采用乘法参数,但它允许你在设置之前自由操纵值:
As you can see it doesn't make sense for a setter to take multiply arguments, but it gives you the freedom to manipulate the value before it is set:
var person = {
surname: "John",
lastname: "Doe",
get fullname() {
return this.surname + " " + this.lastname;
},
set fullname(fullname) {
fullname = fullname.split(' ');
this.surname = fullname[0];
this.lastname = fullname[1];
}
};
console.log(person.fullname); // "John Doe"
person.fullname = "Jane Roe";
console.log(person.surname); // "Jane"
console.log(person.lastname); // "Roe"
这篇关于Uncaught SyntaxError:Setter必须只有一个形式参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!