有几种方法可以实现这个目标。
构造函数和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 src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
(注意:我将Testing
中的第一个t
大写,因为这是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 src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
构造函数和原型对象的对象初始化器
您尝试使用的语法适用于对象初始化器,而不是构造函数。我们可以使用对象初始化器来替换Testing.prototype
,如下所示:
function Testing(){
this.value = "content";
}
Testing.prototype = {
constructor: Testing,
set a(b){
this.value = b;
}
};
实时示例:
function Testing(){
this.value = "content";
}
Testing.prototype = {
constructor: Testing,
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 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 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 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 src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Object.defineProperty
。 - Daniel A. White