Shaolin
07-28-2008, 11:26 PM
Hi Guys
Can some explain what namespaces are? And some examples too please..
Can some explain what namespaces are? And some examples too please..
|
Click to See Complete Forum and Search --> : Namespaces Shaolin 07-28-2008, 11:26 PM Hi Guys Can some explain what namespaces are? And some examples too please.. rnd me 07-29-2008, 12:09 AM javascript doesn't have namespaces. that's a question for a different forum. Shaolin 07-29-2008, 09:21 AM Are you sure ? :confused: What about this link then: http://weblogs.asp.net/mschwarz/archive/2005/08/26/423699.aspx Kor 07-29-2008, 09:37 AM Usual definition: A namespace is an abstract container or environment created to hold a logical grouping of unique identifiers. Javascript has no native namespaces, but it can use constructors to organize custom/private ones. In javascript a namespace is just a custom created object. http://www.dustindiaz.com/namespace-your-javascript/ http://snook.ca/archives/javascript/javascript_name/ toicontien 07-29-2008, 10:43 AM An example of what Kor just posted: // Create MyApplication object, used as a namespace var MyApplication = {}; MyApplication.show = function(msg) { alert(msg); }; // Use a function in this name space (object) MyApplication.show("Example of 'namespaces' in JavaScript"); Declan1991 07-29-2008, 11:18 AM Closures can often be more applicable in JavaScript, they avoid long variable names and global variables. (function(){ var variable = "whatever"; // Do all the code in here })(); alert(variable+""); // undefined There's no way better than another, it depends on what you are doing. Shaolin 07-29-2008, 11:25 AM Thanks guys. So what are the benefits of this method over the conventional ways? Declan1991 07-29-2008, 11:36 AM No globals, and code doesn't conflict. So if I write a function (lets say init()), your init won't conflict with mine. toicontien 07-29-2008, 11:57 AM That's why if you are creating your own function library, defining one object as the "name space" for your functions also works. An issue with using anonymous functions, like what Declan1991 showed, is once the function has fun its course, any data it declares is no longer available. I don't think Declan1991's example is a "JavaScript closure" though. A closure was created, but in a very liberal sense of the word. A closure, rather "function closure" in JavaScript is allows a function to access data that is out of scope: function exampleClosure(message) { return function() { alert(message); }; } var myalert = exampleClosure("You created a function closure!"); myalert(); That alerts the message variable text. When you call the myalert() function like above, there is no direct reference to the message variable you passed to the exampleClosure() function. It just simply exists in the JavaScript Nether and the myalert function is the only thing that has access to it. Closures in the true sense of JavaScript closures are a way to hack in the functionality of private members of a JavaScript class. Just wanted to point out that there is more than one type of "closure" in JavaScript. A closure in the philosophical sense (as in Declan1991's example) and a closure in the literal sense, in the form of a function closure. Declan1991 07-29-2008, 12:27 PM I don't think Declan1991's example is a "JavaScript closure" though. A closure was created, but in a very liberal sense of the word. A closure, rather "function closure" in JavaScript is allows a function to access data that is out of scope. I don't really know what else to call it though. It's neither information hiding or encapsulation, so since it's executing in another scope, I like to call it a closure. toicontien 07-29-2008, 12:34 PM You example was an anonymous function and can be considered a type of encapsulation or closure. Just not a closure in the literal sense. Once an anonymous function is executed, any memory it uses is given back to the system, and the function no longer exists. I just wanted to point out that "closures" do officially exist in JavaScript and are an official part of the language, and that calling your example a closure was more of a name than a language feature. The language feature you used was an anonymous function. Kor 07-29-2008, 01:34 PM http://www.jibbering.com/faq/faq_notes/closures.html Jeff Mott 07-29-2008, 08:29 PM I don't really know what else to call it ... so since it's executing in another scope, I like to call it a closure.As it turns out, "scope" is exactly what you should call it. The function has its own scope. webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |