简体   繁体   English

firefox javascript错误无效标签

[英]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. 有人可以发现这段代码出了什么问题,我的firebug控制台上出现了无效的标签错误。

<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: 例如,如果类型a每个对象都具有这些属性,则可以在原型中设置它们:

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). 请注意,效果通常是相同的,但意义在重要方面有所不同(阅读原型, in运算符和Object.hasOwnProperty()等)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM