1

Hey guys am new to javascript app development..My code

var obj = {
    models: "AN",
    collection: {},
    person: {},

    changeDetails: function(values, babes) {
        obj.person.name = values.name;
        obj.person.age = values.age;
    }

    babe: function() {
        return 5;
    }
};

When i called it like obj.changeDetails({name:"George",age:20}).babe()); it throws me error like Uncaught SyntaxError: Unexpected identifier

Is it possible in javascript to call like objectname.functionname.().anotherfunctionname()??.If its possible please post it as an answer showing its demonstration..

Thanks

2
  • @thefourtheye, not sure it was a good idea to add in that missing comma in your edit. That might have been part of the problem... Commented Jul 24, 2014 at 15:43
  • You can read about function chaining (i.e. in jQuery) to learn more. Commented Jul 24, 2014 at 15:46

6 Answers 6

4

You got 2 syntax errors and 1 logic error

You're missing a comma (,) after

changeDetails: function (values, babes) {
    obj.person.name = values.name;
    obj.person.age = values.age;
}

So it should be

var obj = {
   models: "AN",
    collection: {},
    person: {},
    changeDetails: function(values, babes) {
        obj.person.name = values.name;
        obj.person.age = values.age;
    },
    babe: function() {
        return 5;
    }
};

Also, you got one too-many right parenthesis ()) after

obj.changeDetails({name:"George",age:20}).babe());

It should be

obj.changeDetails({name:"George",age:20}).babe();

Finally, to be able to call the method babe of the object you must return it within the changeDetails function, it makes sense to use this in this context.

Final Solution

var obj = {
    models: "AN",
    collection: {},
    person: {},

    changeDetails: function(values, babes) {
        this.person.name = values.name;
        this.person.age = values.age;
        return this;
    },
    babe: function() {
        return 5;
    }
};
obj.changeDetails({name:"George",age:20}).babe();
Sign up to request clarification or add additional context in comments.

Comments

3

changeDetails doesn't return anything. You can't call .babe() on nothing.

var obj = {
    models: "AN",
    collection: {},
    person: {},

    changeDetails: function(values, babes) {
        obj.person.name = values.name;
        obj.person.age = values.age;

        return obj;
    },

    babe: function() {
        return 5;
    }
};

Now you can chain stuff, since changeDetails returns an object:

obj.changeDetails({name:"George",age:20}).babe();

Comments

2

You simply want to return this:

changeDetails: function(values, babes) {
    obj.person.name = values.name;
    obj.person.age = values.age;
    return this;
},

Comments

0

This is chaining, you must return the obj in every it method:

var obj = {
    models: "AN",
    collection: {},
    person: {},
    changeDetails: function (values, babes) {
        obj.person.name = values.name;
        obj.person.age = values.age;
        return obj;
    },
    babe: function () {
        return 5;
    }
};

1 Comment

how can i call this function ??
0

Yes, it is very well possible. You just need to design your functions in such a way that, they allow chaining of functions possible. For example,

changeDetails: function(values, babes) {
    obj.person.name = values.name;
    obj.person.age = values.age;
    return obj;                            # Return the `obj`
},

Now, the result of the function call changeDetails is obj, which has babe function now. So, you can invoke it like this

obj.changeDetails({
    name: "George",
    age: 20
}).babe();

Also note that, you don't use the second parameter babes in the changeDetails function. So, you can drop that parameter.

Comments

0

I have to say if you're assigning babe status you should at least be returning an 8 - 10 like so - Also you need to return obj in your change details.

var obj = {
    models: "AN",
    collection: {},
    person: {},

    changeDetails: function(values, babes) {
        obj.person.name = values.name;
        obj.person.age = values.age;
        return obj;
    }

    babe: function() {
        return Math.floor((Math.random()*(10-8+1)+8))
    }
};

1 Comment

You're missing a comma.

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.