似乎有很多方法可以设置 JavaScript 应用程序,因此很难确定哪一种是正确的或最好的。以下技术有什么区别或更好的方法吗?
MyNamespace.MyClass = {
someProperty: 5,
anotherProperty: false,
init: function () {
//do initialization
},
someFunction: function () {
//do something
}
};
$(function () {
MyNamespace.MyClass.init();
});
另一种方式:
MyNamespace.MyClass = (function () {
var someProperty = 5;
var anotherProperty = false;
var init = function () {
//do something
};
var someFunction = function () {
//do something
};
return {
someProperty: someProperty
anotherProperty: anotherProperty
init: init
someFunction: someFunction
};
}());
MyNamespace.MyClass.init();
第一种技术感觉更像是一堂课。如果这有什么不同的话,我来自服务器端背景。第二种技术似乎更加多余并且有点尴尬,但我发现这种技术也被广泛使用。有人可以帮助阐明并建议前进的最佳方式吗?我想创建一个应用程序,其中有很多类可以相互通信。
这两件事都不要做。
创建一个 javascript“类”:
var MyClass = function () {
var privateVar; //private
var privateFn = function(){}; //private
this.someProperty = 5; //public
this.anotherProperty = false; //public
this.someFunction = function () { //public
//do something
};
};
MyNamespace.MyClass = new MyClass();
具有静态变量的:
var MyClass = (function(){
var static_var; //static private var
var MyClass = function () {
var privateVar; //private
var privateFn = function(){}; //private
this.someProperty = 5; //public
this.anotherProperty = false; //public
this.someFunction = function () { //public
//do something
};
};
return MyClass;
})();
MyNamespace.MyClass = new MyClass();
使用“构造函数”(所有示例都有一个“构造函数”,这个只有可以使用的参数):
var MyClass = function (a, b c) {
//DO SOMETHING WITH a, b, c <--
var privateVar; //private
var privateFn = function(){}; //private
this.someProperty = 5; //public
this.anotherProperty = false; //public
this.someFunction = function () { //public
//do something
};
};
MyNamespace.MyClass = new MyClass(1, 3, 4);
有了上述所有内容,您就可以做到:
MyNamespace.MyClass.someFunction();
但是你不能做(从外面):
MyNamespace.MyClass.privateFn(); //ERROR!
第一个示例只是一个对象文字 - 它无法实例化并且没有私有成员。第二个示例有一些不正确的语法(
var someProperty: 5
应该是var someProperty = 5
),但使用闭包将内部私有状态封装在自调用匿名函数中。
第二种方法看起来更适合封装私有成员,但可以通过使其成为可实例化的类来使其更加“面向对象”:
MyNamespace.MyClass = function() { ... };
MyNamespace.MyClass.prototype.someProperty = 'foo';
然后您可以使用“new”关键字实例化它:
var aClass = new MyNamespace.MyClass();
aClass.init(...);
我对具有命名空间的可实例化类使用以下语法
var MYNamespace = MYNamespace|| {};
MYNamespace.MyFirstClass = function (val) {
this.value = val;
this.getValue = function(){
return this.value;
};
}
var myFirstInstance = new MYNamespace.MyFirstClass(46);
alert(myFirstInstance.getValue());
为什么你永远不应该使用
return { methodName : methodDelegate}
就像第二个例子:
MyNamespace.MyClass = (function () {
var someProperty = 5;
var init = function () {
//do something
};
return {
someProperty: someProperty
someFunction: someFunction
};
}());
MyNamespace.MyClass.init();
当您使用命名空间时,您必须将其视为声明,而不是实例。
MyNamespace = {};
MyNamespace.sub = {};
MyNamespace.anotherSub = {};
MyNamespace.sub.MyClass = (function () {
var static_var; //static private var
var MyClass2 = function () {
var privateVar; //private
var privateFn = function () { }; //private
this.someProperty = 5; //public
this.anotherProperty = false; //public
this.someFunction = function () { //public
//do something
};
};
return MyClass2;
})();
debugger;
var c1 = new MyNamespace.sub.MyClass();
c1.someProperty = 1; // creates 5->1.
var c2 = new MyNamespace.sub.MyClass();
c2.someProperty = 2; // creates 5->2. c1 is still 1
debugger;
var myClass = function () {
var someProperty = 5;
var anotherProperty = false;
var init = function () {
//do something
};
var someFunction = function () {
//do something
};
return {
someProperty: someProperty,
anotherProperty: anotherProperty,
init: init,
someFunction: someFunction
};
};
MyNamespace.MyClass = myClass();
var c2 = MyNamespace.MyClass;
// how are planning to create one more object, while it's a reference? copy //the whole one?
c2.someProperty = 2; // changes 5 -> 2
var c3 = MyNamespace.MyClass.init(); // create 2 instead of 5
c3.someProperty = 3; // changes c3 and c3 from 2 to 3.
console.log(c2.someProperty + c3.someProperty);
无论模块反模式有多流行。声明使您能够以其他开发人员预期的方式将相同的代码用于不同的实例。代码的质量和阅读的简单性都提高了。任何开发人员的目标都是编写易于阅读的简单代码,而不是更短或 D.R.Y 的代码。 - 但易于其他开发人员阅读和理解。这首先会减少错误的数量。 (c) S.麦康奈尔
如何结合命名空间和类声明:
var ns = { // your namespace
my_value: 1, // a value inside the namespace to avoid polluting
MyClass: function() { // a class inside the namespace
this.class_property: 12,
this.class_method: function() {
console.log("My property: " + this.class_property);
}
},
myFunction: function() { // a function inside a namespace
console.log("I can access namepsace value if you don't use 'new': " + this.my_value);
}
};
获取你的价值:
console.log(ns.my_value);
现在,对于类和函数:如果您使用
new
,则函数内的单词this
将指向它的构造函数。如果不使用 new
,this
将指向命名空间。所以,
使用类:
var obj = new ns.MyClass();
使用功能:
ns.myFunction();
如果构造没有
new
的对象,this
将指向命名空间,因此命名空间将被“销毁”,因为 MyClass.class_property
和 MyClass.class_method
将被添加到其中。
时间已经过去了。我认为现在正确的答案是使用模块。
Edge.mjs
/**
* @typedef {import('./Vertex.mjs').Vertex} Vertex
*/
/**
* An Edge of a @see Graph
*/
class Edge {
/**
* Create an edge.
* @param {number} weight - The weight of the edge.
* @param {Vertex} source - The source vertex of the edge.
* @param {Vertex} target - The target vertex of the edge.
*/
constructor(weight, source, target) {
this.weight = weight
this.source = source
this.target = target
}
}
export { Edge }
Graph.mjs
/**
* @typedef {import('./Edge.mjs').Edge} Edge
* @typedef {import('./Vertex.mjs').Vertex} Vertex
*/
/**
* A Graph of @see Vertex and @see Edge
*/
class Graph {
/**
* Creates a new Graph instance.
* @param {boolean} [isDirected=true] - Is the graph directed (true)?.
* @param {boolean} [acyclic=true] - Is the graph acyclic (true) or can it contain cycles (false).
*/
constructor(isDirected = true, acyclic = true) {
/** @type {Vertex[]} */
this.vertices = []
/** @type {Edge[]} */
this.edges = []
this.isDirected = isDirected
this.acyclic = acyclic
}
...
export { Graph }
index.js
import { Edge } from './Edge.mjs'
import { Vertex } from './Vertex.mjs'
import { Graph } from './Graph.mjs'
export { Edge, Vertex, Graph }
somefile.js
import { Edge as Edge1, Vertex, Graph } from './index.mjs'
import { Edge as Edge2 } from './someotherEdge.mjs'
let edge = new Edge1()
let otherEdge = new Edge2()
...
在 somefile.js 中,您可以避免名称冲突,但可以在同一文件中使用它们。 并且您可以避免尝试使用对象或函数等创建名称空间,并尝试处理随之而来的 jsdoccery。
或者,如果您使用打字稿,您已经享受了命名空间支持和所有打字。