最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

simple javascript, functions in objects - Stack Overflow

programmeradmin1浏览0评论
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.

发布评论

评论列表(0)

  1. 暂无评论