1

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 coming from, its in this block of code.

2
  • jslint is your friend. It will give you line and column numbers of your syntax error. 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). Commented Oct 5, 2011 at 23:21

2 Answers 2

3

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

var IOBreadcrumb = {
  breadcrumbs: [], // <-- comma 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...

Sign up to request clarification or add additional context in comments.

3 Comments

i would like a crumb to have both a title and a url, not sure how i can represent that as a JSON, or as another data structure?
should it be {'title':title,'url':url} since I want to have a title and a url for a crumb?
yep: var crumb = {title: '', url: ''};
2

I believe you want this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.