Can somebody find out whats going wrong with this piece of code, I get Invalid label error on my firebug console.
<script type="text/javascript">
var a = function(){
this.prop1:"value1", //Error here
this.prop2:"value2"
}
var b = new a();
</script>
Try this:
var a = function() {
this.prop1 = "value1";
this.prop2 = "value2";
};
var b = new a();
The :
is only used when using the object literal syntax. For example, if every object of type a
will have these properties, you might set them in the prototype instead:
var a = function() {
// ...
};
a.prototype = {
prop1: "value1",
prop2: "value2"
};
var b = new a();
alert(b.prop1); // alerts "value1"
Note that the effect is often the same but the meaning is different in important ways (read up on prototypes, the in
operator, and Object.hasOwnProperty()
among other things).
You're not defining an object, use = instead:
var a = function() {
this.prop1 = "value1";
this.prop2 = "value2";
}
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