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

javascript - Using nested function with dot operator in js - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Jul 24, 2014 at 16:16 5260452 11.7k8 gold badges55 silver badges65 bronze badges asked Jul 24, 2014 at 15:39 user3869069user3869069 455 bronze badges 2
  • @thefourtheye, not sure it was a good idea to add in that missing ma in your edit. That might have been part of the problem... – Andy Commented Jul 24, 2014 at 15:43
  • You can read about function chaining (i.e. in jQuery) to learn more. – Dmitry Volokh Commented Jul 24, 2014 at 15:46
Add a ment  | 

6 Answers 6

Reset to default 4

You got 2 syntax errors and 1 logic error

You're missing a ma (,) 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();

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();

You simply want to return this:

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

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;
    }
};

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.

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))
    }
};
发布评论

评论列表(0)

  1. 暂无评论