1

I have two constructor function, I just want to use object a method in object b without using new keyword because I dont want to create an object with all its properties when there is no need. for example, inside constructor b I want to use constructor b method.

function a(){
  this.first=function(){
    return 'aaaaaa'
  }
  this.last=function (){
    return 'bbbbb'
  }
}

function b(){
  this.name= //call method last of constructor a.
}    

var person= new b();
console.log(person.name) //bbbbb

Note: Constructor a contains two method. So In given example I do not have any use of method first but may required later on. So I don't want to create new a(). I just want to call it directly

1
  • You can't do that. Move the methods to the prototype. Commented Dec 4, 2014 at 19:01

4 Answers 4

2
function a(){

}

a.prototype.last=function(){
    return 'bbbb'
}

function b(){
    this.name= a.prototype.last()//call method last of constructor a.
}    

var person= new b();
console.log(person.name) //bbbbb
Sign up to request clarification or add additional context in comments.

2 Comments

Your is working well. Just for knowledge what is the difference between assigning a method directly in constructor and adding method using prototype
Well, here is a good detailed answer : stackoverflow.com/questions/2784844/…
1

There are various ways to get its function.

  1. create name space
var a={
  first:function(){
    return 'aaaaaa'
  },
  last:function (){
    return 'bbbbb'
  }
}


function b(){
  this.name= a.last()
}
  1. Add methods to prototype property
var a=function(){
};
a.prototype.first=function(){
   return 'aaaaaa'
};
a.prototype.last=function (){
    return 'bbbbb'
}
function b(){
  this.name= a.prototype.last()
}

finally this will be same as previous one.

  1. call a() and call its methods directly.(without a())

Third is call a() which will create "first" & "last" methods which will be function for its context. and then call your method. But this way is not recommended. for ex.

function a(){
    this.first=function(){
        return 'aaaaaa'
    }
    this.last=function (){
        return 'bbbbb'
   }
}
a()
function b(){   this.name= last()}    

var person= new b(); console.log(person.name) //bbbbb}}

3 Comments

Your first example is not working. When I have removed this. then it is working fine. I Just want to know about your 3 point where you said "call a() and call its methods directly.(without a())", Can you put some example of that
@amit, I made changes to answer, please refer it.
sorry there is no need of this in namespace. sorry for my typo mistake
1
function a(){

  this.first=function(){
    return 'aaaaaa';
  }

  this.last= lastfunc;

}

function b(){
  this.name= lastFunct()
}

var person= new b();
console.log(person.name) //bbbbb

function lastFunct() {
  return 'bbbbb';
}

3 Comments

I know that you are replicating OP answer as close as possible, but might be better to add these functions to the constructor prototype. e.g. b.prototype.name = function..., a.prototype.last = lastfunc, a.prototype.first = function...
@Patrick I agree, but I'll leave it as is since there is another answere with that solution. It's still valid.
Agreed. I provided the comment before the other answer :)
0

first and last don't exist until you create an instance of a. If those are just static methods, why not use an object?

var a = {
  first: function(){
    return 'aaaaaa'
  },
  last: function (){
    return 'bbbbb'
  }
};

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.