simple javascript, functions in objects - Stack Overflow

var rooms = {bedroom: {info: "A dusty bed lies sideways in the midle of the room";north: fun

var rooms = {
bedroom: {
    info: "A dusty bed lies sideways in the midle of the room";

    north: function (  ) {
        //this function returns an error
    }
}
};

I cant work out why this returns an unexpected identifier

-- edit thanks another question

in javascript the good parts he has

var myObject = {
    value: 0;
    increment: function (inc) {
        this.value += typeof inc === 'number' ? inc : 1;
    }
};

is this different to what I am doing?

var rooms = {
bedroom: {
    info: "A dusty bed lies sideways in the midle of the room";

    north: function (  ) {
        //this function returns an error
    }
}
};

I cant work out why this returns an unexpected identifier

-- edit thanks another question

in javascript the good parts he has

var myObject = {
    value: 0;
    increment: function (inc) {
        this.value += typeof inc === 'number' ? inc : 1;
    }
};

is this different to what I am doing?

Share Improve this question edited Dec 8, 2010 at 21:19 fxmle asked Dec 8, 2010 at 21:06 fxmlefxmle 1292 silver badges8 bronze badges 2
  • 1 @fxmile - Looks like a mistake in the book. Is it on page 28 (or close by)? Something similar is listed in the errata for the book for that page. – user113716 Commented Dec 8, 2010 at 21:35
  • ; instead of a ,. That is all. – dumbass Commented Dec 26, 2024 at 10:16
Add a ment  | 

3 Answers 3

Reset to default 4

One should use a , inside of object literals when defining keys and values to separate them, not ;.

var o = { name: 'john', age: 13 }
the room";

There should be ,, not ;.

To answer your second question, it looks there is a typo in the book.

The incorrect example is:

var myObject = {
    value: 0;
    increment: function (inc) {
        this.value += typeof inc === 'number' ? inc : 1;
    }
};

The correct example is:

var myObject = {
    value: 0, 
    increment: function (inc) {
        this.value += typeof inc === 'number' ? inc : 1;
    }
};

Note the ma on the line value: 0,.

As others have mentioned, the ma should be used (instead of the semicolon) for object literals.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745574398a4633874.html

相关推荐

  • simple javascript, functions in objects - Stack Overflow

    var rooms = {bedroom: {info: "A dusty bed lies sideways in the midle of the room";north: fun

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信