admin管理员组文章数量:1289876
While reading "Object-Oriented JavaScript" on Mozilla's website, I stumbled upon this note:
It's important to note that in JavaScript there's no language-level difference between regular objects and namespaces.
However, the note is not clear about what is meant by "language-level difference".
Does it mean there are two ways of writing the same thing? Or that there are two terms that refer to the same thing?
While reading "Object-Oriented JavaScript" on Mozilla's website, I stumbled upon this note:
It's important to note that in JavaScript there's no language-level difference between regular objects and namespaces.
However, the note is not clear about what is meant by "language-level difference".
Does it mean there are two ways of writing the same thing? Or that there are two terms that refer to the same thing?
Share Improve this question edited Jul 14, 2016 at 6:29 XYseven asked Jul 14, 2016 at 6:22 XYseven XYseven 5251 gold badge6 silver badges11 bronze badges 3- 1 How do you define "namespace"? – nnnnnn Commented Jul 14, 2016 at 6:26
- 4 It's simple: There are no namespaces in JavaScript. – Bergi Commented Jul 14, 2016 at 6:30
- 1 There are no "name spaces" in javascript. Objects with properties are used to avoid lots of variables within a particular scope, generally the global scope. These are (incorrectly) called "namespaces" because they act sort of like namespaces. – RobG Commented Jul 14, 2016 at 6:30
4 Answers
Reset to default 17You've taken the quote out of context. The answer is in the paragraph above the quote:
A namespace is a container which allows developers to bundle up functionality under a unique, application-specific name. In JavaScript a namespace is just another object containing methods, properties, and objects.
(Emphasis mine)
The quote:
It's important to note that in JavaScript there's no language-level difference between regular objects and namespaces.
Is just saying that this means a "namespace" is just a object used as a "namespace".
There are no "real" namespaces in JS.
Some languages have an actual namespace which is not the same thing as an object. JavaScript is not one of those languages, so objects are used for this purpose.
For example, the Math
functions like Math.round
and Math.abs
, are all namespaced in the Math
object. They aren't really contextual methods like toString
is (at least not in any implementation I've found), just collected under an object to keep it organized. *
* They are technically methods, because they are accessible by a property on an object (a definition that technically makes all global functions methods because they are available through the global object (ie. window.Function()
)), but unlike methods like toString
, or the methods of most console
implementations like console.log
they do not depend on the object they are called on and the this
value is irrelevant. The Math
object is used purely for namespacing, not because of the context it gives.
First, are you sure you know what a namespace is? Real Quick:
Let's use an analogy. On your computer you have files. But you don't just have files you have folders. Why?
Because if there were no folder every file name would have to be unique and it would be hard to keep things organized.
Same for variables names if you only had global variables. If you could only have global variables every variable name would have to be completely unique.
This would be very difficult to keep track of. Chances are you would probably end up using the same variable name by accident. Your code would act funny and it would be very difficult to track down the problem.
So what's the solution?
That's right put your variables into folders, ahem, sorry, I meant namespaces, put them into namespaces. I gotta figure out how to use the backspace key.
Anyway, languages like C# and Java let you do exactly this:
// C# - example of a language with built in support for namespaces
namespace MySpace {
class MyClass {
}
}
namespace Facebook {
class MyClass {
}
}
There no conflict because the classes are in different namespaces. Then in your code if you wanted to instantiate them you would write something like this:
// C# again (JavaScript code coming up soon - keep scrolling)
var myObject = new Facebook.MyClass();
That's great but JavaScript doesn't have a namespace
keyword. Technically it doesn't have namespaces, what it does have is some really clever programmers.
Their solution? Use objects.
// JavaScript
var MySpace = {};
MySpace.MyFunction = function() {
// insert brilliant code here
};
var Facebook = {};
Facebook.MyFunction = function() {
// insert more brilliant code here
};
Now you have 2 functions with the "same" name that don't get in the way of each other. If you want to call the Facebook
version of MyFunction
you would write code like this:
// JavaScript
Facebook.MyFunction();
As you can see in these examples MySpace
and Facebook
are really objects, but they are objects that we are using only to separate functions and variables, which means we're using them for nothing more than to serve as namespaces.
One Extra Note
A lot of times you will see "namespace" objects declared like this:
var MySpace = MySpace || {};
This means MySpace = MySpace
if the MySpace
object already exists. Otherwise it's assigned a new empty object. This is a way of reusing the MySpace
object/namespace in multiple files.
Each file adds it's own functions and variables to the same "namespace" object. For example:
var MySpace = MySpace || {};
MySpace.a = 10;
var MySpace = MySpace || {};
MySpace.b = 20;
You end up with one MySpace object with the variables a
and b
. This is true even if the code were in different files and even if you reverse the order.
It's important to note that in JavaScript there's no language-level difference between regular objects and namespaces.
It's saying that a "namespace" is not an actual type of component in JavaScript, but rather just another use of a plain old JavaScript object.
本文标签: In JavaScriptwhat is the difference between an object and a namespaceStack Overflow
版权声明:本文标题:In javascript, what is the difference between an object and a namespace? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738486994a2089476.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论