1

In namespace we can use something like this:

var namespace = {};
namespace.something.nested.namespacing();

And also in prototype we can use same like name the namespace:

something.prototype.method();

In both types we are using . notation. So, how can we determine the code is using namespace or prototype?

3
  • 2
    Can you include a more real example, and possibly add why you need to determine this? In the code you provided the difference is obvious as the second case includes "prototype". Commented Jan 28, 2014 at 8:25
  • 2
    There are no namespaces in JavaScript. You can simulate namespaces using JavaScript objects. Commented Jan 28, 2014 at 8:25
  • There is no "namespace" here. Property lookup is on the object first, then its private [[Prototype]], then that object's [[Prototype]] and so on. The object path in the accessing expression is irrelevant to how property lookup proceeds. Commented Jan 28, 2014 at 8:34

3 Answers 3

7

JavaScript doesn't have namespaces. What you're calling a namespace is just an object. It's fairly common to use objects to stand in for namespaces.

Your code:

var namespace = {};
namespace.something.nested.namespacing();

...creates an object and a variable that refers to that object called namespace. Then you add properties to that object (it looks like you've added a property, something, which is also an object, which has a property, nested, which is also an object, which has a property called namespacing which refers to a function).

Your code:

something.prototype.method();

...is also using an object. The above would actually be a really unusual thing to do, you're calling a function (method) by directly referring to the prototype property of what I assume is a function (something). That's not normally what you would do. Normally you'd create an object via the function:

var s = new something();

...and then use the prototype that got assigned to s implicitly:

s.method();

The prototype property on functions can be confusing to people new to JavaScript. It's just a normal property like any other property, which is on function instances. The only thing that makes it special is the new operator: When you call a function via new:

var s = new something();

...the JavaScript engine creates a new, blank, object, then sets its underlying prototype (sometimes called __proto__ but that's not in the spec [yet]) using something.prototype, like this:

// pseudo-code for `var s = new something()`
var tmp = {};                        // New, blank object
tmp.__proto__ = something.prototype; // Assign a prototype to the new object
something.call(tmp);                 // Call `something` with `this` referring to `tmp`
s = tmp;                             // Assign the result to `s`

(There's a detail I left out in the above to avoid confusing matters, to do with the special case where something has an explicit return statement in it that returns an object reference.)

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

Comments

4

Well, namespaces are just objects and prototype objects are objects, so from a syntactic point of view, there is no difference how you access them (foo.bar is called dot notation().

However, they serve two fundamentally different purposes:

  • Namespaces are used to structure your code and to avoid global namespace pollution.
  • Prototypes are used to share methods between multiple instances of the same constructor function.

So you usually don't call a method directly on the prototype because it most likely won't work. The purpose of this method is to be called on an instance of the corresponding constructor function. For example:

function Something() {}
Something.prototype.foo = function() {};

var s = new Something();
s.foo();

Comments

3

The . notation is a very general purpose tool used in javascript for accessing any properties of any object.

var x = {};
x.val = 2;

Your reference to a namespace is just a general purpose object in javascript as there is no actual namespace type in javascript. In other words, plain objects are used to create namespace-like things in javascript. So, as such, object properties are used to keep track of names in the namespace.


The . notation's use with the prototype is one specific instance of accessing properties on a specific type of object (the prototype). A prototype is used on a function object and has a very specific use when creating new instances of that function.

function foo() {
}

foo.prototype.talk = function() {
    alert("hello");
}

var y = new foo();
y.talk();

2 Comments

It means we could only use prototypes only for function object not other types. Thanks.
@NavinRauniyar: No, all objects have a prototype. It's easy to do, but don't confuse the prototype property on functions (which is not the function's prototype) and the prototype of an object (which is not called prototype; it has no externally-visible name at the moment, but in ES6 it will be called __proto__). The prototype property of a function is a reference to an object that will get assigned to objects created if you use that function with the new operator.

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.