简体   繁体   中英

Javascript: Determine if object is being duplicated in memory

Javascript question: How can I determine if "ModuleMethodsConstructor" is being duplicated in memory for each instance of "ModulePropertiesConstructor"? My goal is that each instance use the same methods, and I don't want to use prototype.

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();

Thanks, Bergi I tested using the equality operator as you suggested. I guess I'm trying to rig up a way to get the advantages of closure constructors (private members) and the advantages of prototype (method definitions loaded into memory once and shared among instances). I've read from various sources that marrying the 2 isn't possible ... in the past I've favored closures because early on reading Crockford's book. That said, maybe I'll try prototype for a bit ... Ted PS. and thanks Pointy and RobG

Hey, I just came up with this:

var l = function(){console.log(arguments[0]);} // for shorthand console logging

// 3 constructors for each object is to achieve private members (closure) plus each instance sharing in-memory method definitions (like prototype)

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();

// support chaining? return a reference to the intended callee

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM