javascript-namespaces

How to set up JavaScript namespace and classes properly?

China☆狼群 提交于 2019-11-30 10:13:45
问题 It seems there are so many ways to set up a JavaScript application so it is confusing as to which one is correct or best. Are there any difference to the below techniques or a better way of doing this? MyNamespace.MyClass = { someProperty: 5, anotherProperty: false, init: function () { //do initialization }, someFunction: function () { //do something } }; $(function () { MyNamespace.MyClass.init(); }); Another way: MyNamespace.MyClass = (function () { var someProperty = 5; var anotherProperty

How to set up JavaScript namespace and classes properly?

▼魔方 西西 提交于 2019-11-29 19:52:01
It seems there are so many ways to set up a JavaScript application so it is confusing as to which one is correct or best. Are there any difference to the below techniques or a better way of doing this? MyNamespace.MyClass = { someProperty: 5, anotherProperty: false, init: function () { //do initialization }, someFunction: function () { //do something } }; $(function () { MyNamespace.MyClass.init(); }); Another way: MyNamespace.MyClass = (function () { var someProperty = 5; var anotherProperty = false; var init = function () { //do something }; var someFunction = function () { //do something };

How do I declare a namespace in JavaScript?

别来无恙 提交于 2019-11-25 22:55:57
问题 How do I create a namespace in JavaScript so that my objects and functions aren\'t overwritten by other same-named objects and functions? I\'ve used the following: if (Foo == null || typeof(Foo) != \"object\") { var Foo = new Object();} Is there a more elegant or succinct way of doing this? 回答1: I like this: var yourNamespace = { foo: function() { }, bar: function() { } }; ... yourNamespace.foo(); 回答2: I use the approach found on the Enterprise jQuery site: Here is their example showing how