我试图为我的js"class"分配一个setter:
function testing(){
this.value = "content";
set a(b){
this.value = b;
};
}
var test1 = new testing();
test1.a = "nope";
Run Code Online (Sandbox Code Playgroud)
但是,如果有人告诉我正确的语法是什么,它会SyntaxError: missing ; before statement在网上抛出set a(b){?
有几种方法可以做到这一点.
Object.defineProperty要在像你一样的构造函数中执行它,你可以使用Object.defineProperty或Object.defineProperties.例如:
function Testing(){
this.value = "content";
Object.defineProperty(this, "a", {
set: function(b){
this.value = b;
}
});
}
Run Code Online (Sandbox Code Playgroud)
实例:
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);Run Code Online (Sandbox Code Playgroud)
<!-- 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>Run Code Online (Sandbox Code Playgroud)
(注意:我t在Testing资本中创造了第一个,因为这是JavaScript中构造函数的压倒性惯例.)
Object.defineProperty使用原型或者您可以在原型上定义setter:
function Testing(){
this.value = "content";
}
Object.defineProperty(Testing.prototype, "a", {
set: function(b){
this.value = b;
}
});
Run Code Online (Sandbox Code Playgroud)
实例:
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);Run Code Online (Sandbox Code Playgroud)
<!-- 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>Run Code Online (Sandbox Code Playgroud)
您尝试使用的语法是对象初始值设定项,而不是构造函数.我们可以用一个对象初始化器更换的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;
}
};
Run Code Online (Sandbox Code Playgroud)
实例:
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);Run Code Online (Sandbox Code Playgroud)
<!-- 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>Run Code Online (Sandbox Code Playgroud)
这只是创建一个一次性对象,而不是可以用来构建一堆这些对象的函数:
var t = {
value: "content",
set a(b) {
this.value = b;
}
};
Run Code Online (Sandbox Code Playgroud)
实例:
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);Run Code Online (Sandbox Code Playgroud)
<!-- 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>Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
您不使用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);Run Code Online (Sandbox Code Playgroud)
<!-- 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>Run Code Online (Sandbox Code Playgroud)
最后但并非最不重要的是,您可以使用构建器函数而不使用原型:
function testing() {
return {
value: "content",
set a(b){
this.value = b;
}
};
}
Run Code Online (Sandbox Code Playgroud)
您不使用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);Run Code Online (Sandbox Code Playgroud)
<!-- 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>Run Code Online (Sandbox Code Playgroud)