Javascript问题:如何确定“ModuleMethodsConstructor”是否在内存中为“ModulePropertiesConstructor”的每个实例复制?我的目标是每个实例使用相同的方法,我不想使用原型。
function ModuleMethodsConstructor() {
var referenceToInstanceProperties = arguments[0];
var privateMethods = {}; //
var publicMethods = {};
publicMethods.setProperty1 = function(){
referenceToInstanceProperties.property1 = arguments[0];
};
publicMethods.getProperty1 = function(){
console.log(referenceToInstanceProperties.property1);
};
privateMethods.privateMethod = function() {
console.log('privateMethod');
};
return {'privateMethods':privateMethods,'publicMethods':publicMethods};
}; // ModuleMethodsConstructor
function ModulePropertiesConstructor() {
var properties = {
'property1' : 'value1',
};
var returned = ModuleMethodsConstructor(properties);
var privateMethods = returned.privateMethods;
var publicMethods = returned.publicMethods;
return publicMethods;
}; // ModulePropertiesConstructor
var instance1 = ModulePropertiesConstructor();
var instance2 = ModulePropertiesConstructor();
答案 0 :(得分:0)
谢谢,Bergi我按照你的建议使用了相等运算符进行了测试。我想我正试图找到一种方法来获得闭包构造函数(私有成员)的优势和原型的优点(方法定义一次加载到内存中并在实例之间共享)。我从各种消息来源中读到,结婚2是不可能的......过去我一直喜欢关闭,因为早在阅读Crockford的书时。也就是说,也许我会试一下原型...... Ted PS。并感谢Pointy和RobG
嘿,我刚想出来了:var l = function(){console.log(arguments [0]);} //用于速记控制台记录
//每个对象的3个构造函数是实现私有成员(闭包)加上每个实例共享内存中的方法定义(如原型)
function ModulePrivateMethodsConstructor(){
var properties = {};
var privateMethods = {};
var publicMethods = {}; // will become private methods of instance
publicMethods.test = function() {
l('test');
}; //
return publicMethods;
}; // ModulePrivateMethodsConstructor
var modulePrivateMethods = ModulePrivateMethodsConstructor();
function ModulePublicMethodsConstructor(){
var properties = {};
var privateMethods = {};
var publicMethods = {}; // will become public methods of instance
publicMethods.test = function() {
l('test');
}; // publicMethods.test
publicMethods.logID = function() {
// l(this); // this refers to ModulePublicMethodsConstructor
l(arguments[0].instanceProperties.id);
}; // publicMethods.logID
return publicMethods;
}; // ModulePublicMethodsConstructor
var modulePublicMethods = ModulePublicMethodsConstructor();
function ModulePropertiesConstructor(){
var properties = {};
properties.id = arguments[0].id;
var privateMethods = {};
var publicMethods = {};
var instancePublicMethods = {};
privateMethods = arguments[0].modulePrivateMethods;
publicMethods = arguments[0].modulePublicMethods;
// each method needs this signature so the methods have access to instance properties
instancePublicMethods.logID = function(){
publicMethods.logID({'args':arguments[0],'instanceProperties':properties});
}; //
return instancePublicMethods;
}; // ModulePropertiesConstructor
var modulePropertiesConstructorArgs = {'modulePrivateMethods':modulePrivateMethods,'modulePublicMethods':modulePublicMethods};
modulePropertiesConstructorArgs.id ='instance1'; var instance1 = ModulePropertiesConstructor(modulePropertiesConstructorArgs);
modulePropertiesConstructorArgs.id ='instance2'; var instance2 = ModulePropertiesConstructor(modulePropertiesConstructorArgs);
instance1.logID();
instance2.logID();
instance1.logID();
//支持链接?返回对目标被调用者的引用