我试图为我的js“class”分配一个setter:
function testing(){
this.value = "content";
set a(b){
this.value = b;
};
}
var test1 = new testing();
test1.a = "nope";
但它在行SyntaxError: missing ; before statement
上抛出set a(b){
可能有人告诉我正确的语法是什么吗?
答案 0 :(得分:8)
有几种方法可以做到这一点。
Object.defineProperty
要在您拥有的构造函数中执行此操作,您可以使用Object.defineProperty
或Object.defineProperties
。例如:
function Testing(){
this.value = "content";
Object.defineProperty(this, "a", {
set: function(b){
this.value = b;
}
});
}
直播示例:
function Testing() {
this.value = "content";
Object.defineProperty(this, "a", {
set: function(b) {
this.value = b;
}
});
}
var t = new Testing();
snippet.log("t.value before: " + t.value);
t.a = "new content";
snippet.log("t.value after: " + t.value);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
(注意:我在t
首都中创建了第一个Testing
,因为这是JavaScript中构造函数的压倒性约定。)
Object.defineProperty
或者您可以在原型上定义setter:
function Testing(){
this.value = "content";
}
Object.defineProperty(Testing.prototype, "a", {
set: function(b){
this.value = b;
}
});
直播示例:
function Testing(){
this.value = "content";
}
Object.defineProperty(Testing.prototype, "a", {
set: function(b){
this.value = b;
}
});
var t1 = new Testing();
snippet.log("t1.value before: " + t1.value);
t1.a = "new content";
snippet.log("t1.value after: " + t1.value);
var t2 = new Testing();
snippet.log("t2.value before: " + t2.value);
t2.a = "new content";
snippet.log("t2.value after: " + t2.value);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
您尝试使用的语法是对象初始值设定项,而不是构造函数。我们可以使用对象初始值设定项替换 Testing.prototype
,如下所示:
function Testing(){
this.value = "content";
}
Testing.prototype = {
constructor: Testing, // The old object had this, so let's maintain it
set a(b){
this.value = b;
}
};
直播示例:
function Testing(){
this.value = "content";
}
Testing.prototype = {
constructor: Testing, // The old object had this, so let's maintain it
set a(b){
this.value = b;
}
};
var t1 = new Testing();
snippet.log("t1.value before: " + t1.value);
t1.a = "new content";
snippet.log("t1.value after: " + t1.value);
var t2 = new Testing();
snippet.log("t2.value before: " + t2.value);
t2.a = "new content";
snippet.log("t2.value after: " + t2.value);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
这只是创建一个一次性对象,而不是可用于构建一堆这些对象的函数:
var t = {
value: "content",
set a(b) {
this.value = b;
}
};
直播示例:
var t = {
value: "content",
set a(b) {
this.value = b;
}
};
snippet.log("t.value before: " + t.value);
t.a = "new content";
snippet.log("t.value after: " + t.value);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Object.create
构造函数(与new
一起使用的函数)并不是城里唯一用于创建具有原型的对象的游戏。如果您愿意,也可以通过Object.create
:
var testingPrototype = {
set a(b){
this.value = b;
}
};
function testing(){
var obj = Object.create(testingPrototype);
obj.value = "content";
return obj;
}
你不要使用new
,因为他们自己创建了对象:
直播示例:
var testingPrototype = {
set a(b){
this.value = b;
}
};
function testing(){
var obj = Object.create(testingPrototype);
obj.value = "content";
return obj;
}
var t1 = testing();
snippet.log("t1.value before: " + t1.value);
t1.a = "new content";
snippet.log("t1.value after: " + t1.value);
var t2 = testing();
snippet.log("t2.value before: " + t2.value);
t2.a = "new content";
snippet.log("t2.value after: " + t2.value);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
最后但并非最不重要的是,您可以使用构建器功能而不使用原型:
function testing() {
return {
value: "content",
set a(b){
this.value = b;
}
};
}
您不会将new
与以下内容一起使用:
直播示例:
function testing(){
return {
value: "content",
set a(b){
this.value = b;
}
};
}
var t1 = testing();
snippet.log("t1.value before: " + t1.value);
t1.a = "new content";
snippet.log("t1.value after: " + t1.value);
var t2 = testing();
snippet.log("t2.value before: " + t2.value);
t2.a = "new content";
snippet.log("t2.value after: " + t2.value);
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
答案 1 :(得分:0)