I tried to assign a setter for my js "class":
function testing(){
this.value = "content";
set a(b){
this.value = b;
};
}
var test1 = new testing();
test1.a = "nope";
But it throws out SyntaxError: missing ; before statement
on line set a(b){
could some one tell me how the correct syntax is ?
There are several ways to do this.
Object.defineProperty
To do it within a constructor function like you have, you'd use Object.defineProperty
or Object.defineProperties
. For instance:
function Testing(){
this.value = "content";
Object.defineProperty(this, "a", {
set: function(b){
this.value = b;
}
});
}
Live Example:
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>
(Note: I made the first t
in Testing
capital because that's the overwhelming convention in JavaScript for constructor functions.)
Object.defineProperty
using prototypeOr you might define the setter on the prototype:
function Testing(){
this.value = "content";
}
Object.defineProperty(Testing.prototype, "a", {
set: function(b){
this.value = b;
}
});
Live Example:
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>
The syntax you were trying to use is for object initializers, not constructor functions. We can use an object initializer to replace the Testing.prototype
, like this:
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;
}
};
Live Example:
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>
This just creates a one-off object, not a function that you can use to build a bunch of these objects:
var t = {
value: "content",
set a(b) {
this.value = b;
}
};
Live Example:
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
for the prototypeConstructor functions (functions you use with new
) are not the only game in town for creating objects with prototypes behind them. You can do that with a builder function as well if you like, via Object.create
:
var testingPrototype = {
set a(b){
this.value = b;
}
};
function testing(){
var obj = Object.create(testingPrototype);
obj.value = "content";
return obj;
}
You don't use new
with these, because they create the object themselves:
Live Example:
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>
Last but not least, you can use a builder function and not use a prototype:
function testing() {
return {
value: "content",
set a(b){
this.value = b;
}
};
}
You don't use new
with these either:
Live Example:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With