In javascript objects, do & ldquo; Static & rdquo; Do prototype functions and functions share the same namespace?

advertisements

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.


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.