简体   繁体   中英

A few questions about Javascripts Prototype

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 } 

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/

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' by this.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'
  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() { /* ... */ };

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM