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

oop - Unexpected identifier in Javascript Object - Stack Overflow

programmeradmin0浏览0评论

I am creating the following object:

  var IOBreadcrumb = {
    breadcrumbs: []

    add: function(title, url){
      var crumb = {title, url};
      this.breadcrumbs.push(crumb);
    }
  };

I am getting an unexpected identifier error. Not really sure where it is ing from, its in this block of code.

I am creating the following object:

  var IOBreadcrumb = {
    breadcrumbs: []

    add: function(title, url){
      var crumb = {title, url};
      this.breadcrumbs.push(crumb);
    }
  };

I am getting an unexpected identifier error. Not really sure where it is ing from, its in this block of code.

Share Improve this question asked Oct 5, 2011 at 23:08 Sheehan AlamSheehan Alam 61k126 gold badges359 silver badges561 bronze badges 2
  • jslint is your friend. It will give you line and column numbers of your syntax error. – hurrymaplelad Commented Oct 5, 2011 at 23:13
  • @hurrymaplelad No need for an external tool. The browser itself does give you the line number of the error (and other details). – Šime Vidas Commented Oct 5, 2011 at 23:21
Add a ment  | 

2 Answers 2

Reset to default 3

You need a ma between members of your object, which is the cause of the error you cite. You also need to put a colon, rather than a ma, between the key-value pair in the crumb object.

var IOBreadcrumb = {
  breadcrumbs: [], // <-- ma here

  add: function(title, url){
    var crumb = {title: url}; // <-- colon here
    this.breadcrumbs.push(crumb);
  }
};

If you want an object where there are two members, one the title and one the URL, you may want something like this:

var crumb = {
  title: title,
  url: url
};

I don't know whether that would work with your breadcrumbs setup...

I believe you want this:

var IOBreadcrumb = {
    breadcrumbs: [],
    add: function ( title, url ) {
        var crumb = {};
        crumb[ title ] = url;
        this.breadcrumbs.push( crumb );
    }
};
发布评论

评论列表(0)

  1. 暂无评论