0

I want to be able to assign function from another class name in javascript. Here is what i mean in script:

classA = function() {

     this.direction = 1;
     this.someFunction = function() {
          this.direction = 0;
     }   

}

classB = function() {

    this.direction = 2;
    this.anotherFunction = classA.function

}

objA = new classA;
objB = new classB;
objB.anotherFunction();
console.log(objB.direction); // 0
console.log(objA.direction); // 1

How can I achieve that? I can't find much information about the prototype variable of javascript but i feel like this is possible with that.

EDIT: Fixed a typo

6
  • What is this? objB = new objB;? Commented May 14, 2014 at 6:34
  • oops. it was a typo. fixed now Commented May 14, 2014 at 6:35
  • 1
    It's not clear what you want to achieve. Do want to "borrow" a function from classA to be used in classB? Commented May 14, 2014 at 6:37
  • thats exactly what I want to do but inside the class definition. not like objB.anotherFunction = objA.someFunction Commented May 14, 2014 at 6:38
  • I could answer you ,the problem is down the road ,all solutions have side effects ,if you dont understand "this" in javascript you should learn about "this" is javascript first. and prototypal inheritance.A quick fix would be a disservice to you. Commented May 14, 2014 at 6:39

4 Answers 4

2

Try it in the following way:

var classA = function() {
     this.direction = 1;
     this.someFunction = function() {
         this.direction = 0;
     };
};

var classB = function() {
    this.direction = 2;
    this.anotherFunction = new classA().someFunction;
};

var objA = new classA;
var objB = new classB;
objB.anotherFunction();
console.log(objB.direction); // 0
console.log(objA.direction); // 1
Sign up to request clarification or add additional context in comments.

2 Comments

this one fits my situation better as I should have used prototypes but have already written 700 lines of code using the other way...
FYI this way uses more memory and is slower. Hopefully not a problem for your code.
1

This is what I consider the right way to do this...

var classA = function() {
     this.direction = 1;
}

classA.prototype.someFunction = function() {
     this.direction = 0;
}   

var classB = function() {
    this.direction = 2;
}

classB.prototype.anotherFunction = classA.prototype.someFunction;

objA = new classA();
objB = new classB();
objB.anotherFunction();
console.log(objB.direction);
console.log(objA.direction);

1 Comment

As mpm said, if you want to write your own classes, read up online about Javascript classes specifically, and about Javascript's prototypal inheritance. It would also help you to look at some open-source JS projects.
1

You can use the prototype to define the functions:

var classA = function () {
    this.direction = 1;
}
classA.prototype.someFunction = function () {
    this.direction = 0;
}


var classB = function () {
    this.direction = 2;
}
classB.prototype.anotherFunction = classA.prototype.someFunction;

objA = new classA();
objB = new classB();
objB.anotherFunction();
console.log(objB.direction); // 0
console.log(objA.direction); // 1

3 Comments

Yup, don't worry, not accusing you of anything! :)
I would have but I have already defined all my functions using this.func = function() {}
Are there so many of them that refactoring is out of the question?
0

Edit: Looks like all the answers are similar.

Here's a fiddle that demonstrates using the prototype object: http://jsfiddle.net/AZ757/

function ClassA(){
    this.direction = 1;
}

// Expose method
ClassA.prototype.someFunction = function(){
    this.direction = 0;   
}

function ClassB(){
    this.direction = 2;

    // Instance property
    this.anotherFunction = ClassA.prototype.someFunction;
}

// Instances
objA = new ClassA();
objB = new ClassB();

console.log(objB.direction); // Outputs 2
console.log(objA.direction); // Outputs 1

objB.anotherFunction(); // Sets to 0

console.log(objB.direction); // Outputs 0

objA.someFunction(); // Set to 0

console.log(objA.direction); // Outputs 0

As pointed out by Hamza in the comments, I've made the mistake of assigning ClassA.prototype.someFunction to this.anotherFunction (copy/paste laziness).

Ideally, we want to go with the approach others have suggested (assigning someFunction to ClassB's prototype object), since this avoids creating those unique instances every time we call new. However, I'd like to leave it here since it does illustrate a slight difference between how others have suggested and this answer.

Thanks!

2 Comments

You should probably be setting ClassB.prototype.anotherFunction.
Hah, I probably should. Just to mix things up, I'll keep it as an instance property (and make a note of how not to do this...)

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.