有几种方法可以做到这一点。
构造函数和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>