1

regardless of the wisdom of such a naming scheme, is the following valid?

var F = function() {
  this.f1 = function() {}
}
F.f1 = function() {}

// "static" call
var v1 = F.f1();

// prototype call
var f = new F();
var v2 = f.f1();

it seems it should be ok since, for example, var and this variables within an object function do not share the same space.

5
  • 1
    There is actually nothing prototype related in this example. Commented Jul 21, 2011 at 8:16
  • @Felix Kling - then I am very confused. I had thought that f1() in var F = function(){ this.f1(){...} } was identical to F.prototype.f1 = function(){...} Commented Jul 21, 2011 at 8:43
  • @August Lilleaas - valid in that their namespace does not overlap, qed, they are two distinct functions Commented Jul 21, 2011 at 8:46
  • 1
    @ccyoung: No it is not. F.prototype is one object from which all new objects will inherit. But with this.f1 = ... you are assigning the function to each new element (each element has its own function). It will look the same but the object structure is different. Have a look at the output (console) of this example: jsfiddle.net/fkling/5ewLe Commented Jul 21, 2011 at 8:53
  • @Felix Kling - this makes sense - thanks for the clarity! Commented Jul 21, 2011 at 9:02

2 Answers 2

2

Yes, that is valid.

In your example, F is a function which you assign a property called f1 which is a function itself. If you were to change the code to read f = new F(), then f is an object which inherits from F's prototype, but they're still two distinct objects.

Sign up to request clarification or add additional context in comments.

2 Comments

It does not have F at its prototype (at least I find the wording confusing). It inherits from F's prototype...
Thanks Felix. I work with this stuff daily and I still find it confusing.
2

It is valid.
Like the others have stated, there are no prototype related issues here.
You are attaching 2 propertyes to 2 different objects:

  • the so called "static" function is attached to the function definition (F)
  • the so called "public" function is attached to the object returned by the constructor (new F())

So, since F !== new F(), they are 2 different thing with different props.

If you want to make use of the prototypal inheritance, you can consider the following example:

 var F = function(){}
 // function visible to all of F's instances
 F.prototype.f1 = function(){console.log("I'm inherited!");}
 // a so called "static" function
 F.f1 = function(){console.log("I'm static!");}
 var instance1 = new F();
 var instance2 = new F();
 // function visible only to this instance of F
 instance1.f1 = function(){console.log("I'm visible only to this instance of F");}

4 Comments

what is the difference between the "private" f1 (as defined above) and the "prototype" f1 as you defined? would there would be namespace conflicts.
it seems to me "private" is incorrect from the traditional object nomenclature. anyone can access this.f1 = , whereas a truly "private" declaration would be var f1 =.
Define conflict:P. The "private" function overwrites the prototype one (only for that current instance). When you call obj.method(), if the method is not defined in the current object (such as our "private"), javascript looks up obj's prototypal chain and searches for that method.
sry, i ment to write public (i was thinking that it's private in the way that is different only for that instance). corrected the answer. sry 'bout that

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.