Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid label on firefox javascript error

Tags:

javascript

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>
like image 636
indianwebdevil Avatar asked Dec 17 '10 14:12

indianwebdevil


2 Answers

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).

like image 136
psmay Avatar answered Oct 13 '22 22:10

psmay


You're not defining an object, use = instead:

var a = function() {
    this.prop1 = "value1";
    this.prop2 = "value2";
}
like image 28
Tatu Ulmanen Avatar answered Oct 14 '22 00:10

Tatu Ulmanen