码:
var thing = { sanity:0,init:function(){ //code },send:function(){ //code } } function thing(){ this.sanity = 0; this.init = function(){ //code }; this.send = function(){ //code }; } thing1 = new thing();
解决方法
静态对象或对象文字不需要与新操作符进行实例化,也可以像单例一样.请考虑以下示例:
码:
var staticObject1 = { a: 123,b: 456 }; var staticObject2 = staticObject1; console.log(staticObject1,staticObject2); staticObject2.b = "hats"; console.log(staticObject1,staticObject2);
输出:
Object a=123 b=456 Object a=123 b=456 Object a=123 b=hats Object a=123 b=hats
请注意,更改staticObject2.b也会影响staticObject1.b.然而,这可能并不总是期望的效果.许多库,如Dojo,提供了一种对象克隆方法,可以减轻这种情况,如果你想制作一个静态对象的副本.继续前面的例子,考虑以下几点:
码:
var staticObject3 = dojo.clone(staticObject1); // See the doc in the link above staticObject1.a = "pants"; console.log(staticObject1,staticObject2,staticObject3);
输出:
Object a=pants b=hats Object a=pants b=hats Object a=123 b=hats
请注意,staticObject1和staticObject2的成员的值是相同的,而staticObject3不受这些其他对象的更改的影响.
静态对象也可用于创建项目或库命名空间,而不是填充全局范围,并且像没有人的业务一样提升兼容性.
这在创建需要可移植性或互操作性的库时很有用.这在Dojo,YUI和ExtJ等流行的库中都可以看出,其中所有或大多数方法分别称为dojo.examplMethod(),YUI(),exampleMethod()或Ext.exampleMethod().
静态对象也可以被认为与C/C++中的结构类似.
类构造函数/实例化对象
JavaScript中的类基于原型继承,这是一个更复杂的主题,可以阅读关于here,here和here.
与静态对象相反,由于JavaScript的closuer属性,此对象创建方法为私有作用域对象成员和方法提供了独一无二的机会.请考虑私人类成员的以下示例:
码:
var SomeObject = function() { var privateMember = "I am a private member"; this.publicMember = "I am a public member"; this.publicMethod = function() { console.log(privateMember,this.publicMember); }; }; var o = new SomeObject(); console.log(typeof o.privateMember,typeof o.publicMember); o.publicMethod();
输出:
undefined string I am a private member I am a public member
请注意,typeof o.privateMember是“undefined”,在对象外部不可访问,但是来自内部.
私有的方法也可以做到,但并不是直截了当,而且还是很容易实现的.问题在于,私有方法内部的值默认为窗口,必须应用两种技术之一,以确保它引用我们正在使用的对象(在本例中为SomeObject的实例).请考虑以下示例:
码:
var SomeObject = function() { var privateMember = "I am a private member"; var privateMethod = function() { console.log(this.publicMember); }; this.publicMember = "I am a public member"; this.publicMethod = function() { console.log(privateMember,this.publicMember); }; this.privateMethodWrapper = function() { privateMethod.call(this); } }; var o = new SomeObject(); console.log(typeof o.privateMethod,typeof o.publicMethod,typeof o.privateMethodWrapper); o.privateMethodWrapper();
输出:
undefined function function I am a public member
注意,使用privateMethodWrapper(),privatemethod是通过调用和传递函数的上下文来执行的.这一切都很好然而,以下技术是最好的(在我看来),因为它简化了呼叫范围并产生了相同的结果.以前的例子可以改为:
码:
var SomeObject = function() { var self = this; var privateMember = "I am a private member"; var privateMethod = function() { console.log(self.publicMember); }; this.publicMember = "I am a public member"; this.publicMethod = function() { console.log(privateMember,this.publicMember); }; this.privateMethodWrapper = function() { privateMethod(); } }; var o = new SomeObject(); console.log(typeof o.privateMethod,typeof o.privateMethodWrapper); o.privateMethodWrapper();
输出:
undefined function function I am a public member
我现在要把它粘贴到我的博客中.希望有帮助;)