2

I was wondering if it is possible to do the following:

worker.name("John").salary(100)

Basically, this changes the worker's(John's) salary to 100.

Is there a way to define a function in a function?

Can you tell me the method and explain it?

Sampson
269k76 gold badges546 silver badges570 bronze badges
asked Mar 22, 2014 at 5:25
6
  • 2
    Ans :yes..It possible Commented Mar 22, 2014 at 5:27
  • @Pilot Can you tell me the method and explain it? Commented Mar 22, 2014 at 5:27
  • That's not a function in a function. Just return something you can call the next method on. Method chaining. Commented Mar 22, 2014 at 5:28
  • define object worker with nested method returning method again Commented Mar 22, 2014 at 5:28
  • Have you checked: phpied.com/3-ways-to-define-a-javascript-class ? Commented Mar 22, 2014 at 5:28

3 Answers 3

4

This is often times called chaining. Essentially, each method returns the parent object:

var worker = {
 name: function ( name ) {
 this._name = name;
 return this;
 },
 salary: function ( salary ) {
 this._salary = salary;
 return this;
 }
};
worker.name("Jonathan").salary("1ドル");
alert( worker._name );
alert( worker._salary );

You'll note that each method returns the object. This is made very clear in the following screenshot. If we console output the results of calling the name method, we see that the entire worker object is returned:

enter image description here

answered Mar 22, 2014 at 5:32
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know this was possible until now.
3

Create a constructor function like:

var Worker = function(){
};
Worker.prototype.name = function (name){
 this.name = name;
 return this;
};
Worker.prototype.salary = function (salary){
 this.salary = salary;
 return this;
}

Now above constructor function can be used as:

var worker = new Worker();
worker.name("John Doe").salary(100);
answered Mar 22, 2014 at 5:34

2 Comments

Can you just use "Worker().name("JOHN").salary(100)"?
Nope, that wont be possible as I have used constructor pattern. For that you can check @Jonathan Sampson solution.
1

This is possible:

var worker = {
 nameVar: null,
 salaryVar: null,
 name: function(name) {
 this.nameVar = name;
 return this;
 },
 salary: function(salary) {
 this.salaryVar = salary;
 return this;
 }
}

Each method modifies the object, and returns this, which is the object. Then you can call another method, like in your example, without writing the object name explicitly.

Alternatively, you can implement a .clone method, and instead of this, return the clone with a modified property. This would be somewhat similar to the way jQuery works.

answered Mar 22, 2014 at 5:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.