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";
}
It should be '=' not ':'
<script type="text/javascript">
var a = function(){
this.prop1="value1", //Error here
this.prop2="value2"
}
var b = new a();
</script>
Hope this helps.
Should be like this. You should use equal sign because you are not defining an object, you are in a function. And should use ; at the end of line not ,
var a = function(){ this.prop1 = "value1"; //Error here this.prop2 = "value2"; } var b = new a();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.