indianwebdevil
indianwebdevil

Reputation: 5127

invalid label on firefox javascript error

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>

Upvotes: 5

Views: 6130

Answers (4)

Fatih Acet
Fatih Acet

Reputation: 29529

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();

Upvotes: 0

psmay
psmay

Reputation: 1021

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

Upvotes: 8

Manu
Manu

Reputation: 4123

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.

Upvotes: 0

Tatu Ulmanen
Tatu Ulmanen

Reputation: 124768

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

var a = function() {
    this.prop1 = "value1";
    this.prop2 = "value2";
}

Upvotes: 2

Related Questions