2

I am coming at Javascript from a classical OOP background and am having trouble with understanding the prototype.

Given the code example below:

  1. How would I call/execute bar in foo?
  2. why use the "privileged" function instead of putting it on the prototype?
  3. This is probably answered in Q1 but can bar1 and bar2 call each other?

    function foo()
     {
     this.property = "I'm a property";
     this.privileged = function()
     {
     // do stuff
     }
     }
     foo.prototype.bar = function()
     {
     // do stuff
     }
     foo.prototype.bar2 = function()
     {
     // stuff
     }
    
Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Jun 29, 2012 at 11:44
0

4 Answers 4

3

There's a lot of FUD about this to this day.

1). Simple usage:

var xxx = new foo(); // Create instance of object.
xxx.privileged(); // Calls the internal closure.
xxx.bar(); // First looks for internals, then looks to the prototype object.

2). This basically creates a closure that can only be modified on the instance. Not a private at all (since anything can talk to it through the object instance), but rather an individual copy of a function, where each instance of the object gets a new copy of the function. You can alter the function itself, but you cannot alter it globally. I'm not a big fan of this method of creating things.

3). Yes:

foo.prototype.bar = function(){
 this.bar2();
}
foo.prototype.bar2 = function(){
 this.bar();
}
// Although... NEVER do both. You'll wind up in a circular chain.

For edification, I built you a fiddle which should help show how things get called: http://jsfiddle.net/7Y5QK/

answered Jun 29, 2012 at 12:21
Sign up to request clarification or add additional context in comments.

1 Comment

The jsFiddle is excellent! I read your answer and thought "yeah, makes sense". Started to think about the "but what if..." and the jsFiddle allowed me to test it all out. Excellent.
2

1) frist is some as Clyde Lobo's an answer:

var f = new foo();
f.bar();

2) write a function on consturctor (privileged), every instance will create a new one, if defined a method on prototype, every instance share the same prototype's method:

var f1 = new foo(), // instance 1
 f2 = new foo(); // instance 2
f1.privileged === f2. privileged // -> false , every instance has different function
f1.bar === f2.bar // -> true, every instance share the some function

3) You can call bar2 in bar' bythis.bar()`, code like this:

function foo() {
 this.property = "I'm a property";
 this.privileged = function() {
 // do stuff
 };
}
foo.prototype.bar = function() { // defined a method bar
 alert('bar called');
 this.bar2(); // call bar2
};
foo.prototype.bar2 = function() { // defined a method bar2
 alert('bar2 called');
};
var f = new foo();
f.bar(); // alert 'bar called' and 'bar2 called'
f.bar2(); // alert 'bar2 called'
answered Jun 29, 2012 at 12:35

Comments

1
  1. You can execute bar in foo by doing this.bar(); but it will only work if you use new foo(); and have an object inheriting from foo. Otherwise, if you just call foo();, this will point to the global object.

  2. It's essentially the same thing only the properties and methods you give the inheriting object inside the function have access to any arguments you pass to foo.

  3. Yes, they can call each other since functions are "hoisted". They have access to all the variables and objects in their nearest outer scope, and in their functional context.

You actually have syntax errors in your code. Where you were assigning the prototype you didn't create a function. You meant:

foo.prototype.bar = function() { /* ... */ };
answered Jun 29, 2012 at 11:53

Comments

0

1.You can create an instance of foo like

var f = new foo();

and then call f.bar() and f.bar2()

points 2 & 3 are already explained by David

answered Jun 29, 2012 at 12:06

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.