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

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
Jitender
  • 7,593
  • 30
  • 104
  • 210

4 Answers4

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
juvian
  • 15,875
  • 2
  • 37
  • 38
  • Your is working well. Just for knowledge what is the difference between assigning a method directly in constructor and adding method using prototype – Jitender Dec 04 '14 at 19:07
  • Well, here is a good detailed answer : http://stackoverflow.com/questions/2784844/setting-javascript-prototype-function-within-object-class-declaration – juvian Dec 04 '14 at 19:09
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';
}
Ceres
  • 3,524
  • 3
  • 18
  • 25
  • 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 Dec 04 '14 at 19:04
  • @Patrick I agree, but I'll leave it as is since there is another answere with that solution. It's still valid. – Ceres Dec 04 '14 at 19:06
  • Agreed. I provided the comment before the other answer :) – Patrick Dec 04 '14 at 19:08
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}}
Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
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'
  }
};
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143