Domanda

Will this calc function be defined once (put on the prototype chain for the 'obj' variable)? If not, then how can I get the calc function on the prototype chain so all instantiations of Obj will reference the same calc function?

I tried assigning the calc function to Obj.prototype.calc, but then when 'obj' is created, it can't find it in the creation process.

window.onload = function () {
    var Obj = function (obj) {
        var calc = function (o) {
            for (p in o) {
                if (o.hasOwnProperty(p) && typeof o[p] === 'function') {
                    o[p] = o[p]();
                }
            }
            return o;
        };
        return calc(obj);
    };

    function test() {
        var obj = new Obj({
            a: 1,
            b: 2,
            c: function () {
                return this.a + this.b;
            },
            d: function () {
                return this.b * this.c;


   }
    });
    window.console.log(obj.a); // 1
    window.console.log(obj.b); // 2
    window.console.log(obj.c); // 3
    window.console.log(obj.d); // 6
}
test();

}

The purpose of the Obj constructor is creating an object literal type syntax when I define many versions of 'obj' but allowing the use of the 'this' keyword to calculate properties in terms of other properties in each object.

Here is the version with the prototype definition which doesn't work:

<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function () {
    var Obj =   function (obj) {
                    return calc(obj);
                };
    Obj.prototype.calc = function (o) {
                            for (p in o) {
                                if (o.hasOwnProperty(p) && typeof o[p] === 'function') {                        
                                    o[p] = o[p]();
                                }
                            }
                            return o;
                        };
    function test() {
        var obj = new Obj({
            a:      1,
            b:      2,
            c:      function () {return this.a + this.b;},
            d:      function () {return this.b * this.c;}
        });
       window.console.log(obj.a); 
       window.console.log(obj.b); 
       window.console.log(obj.c); 
       window.console.log(obj.d); 
    }
    test();   

}
</script>
</head>
<body>
</body>
</html>
È stato utile?

Soluzione

Will this calc function be defined once (put on the prototype chain for the 'obj' variable)?

No, it gets re-created every time your Obj function is called. (Also note that as Pointy, er, pointed out, you're falling prey to The Horror of Implicit Globals with your calc symbol.)

If not, then how can I get the calc function on the prototype chain so all instantiations of Obj will reference the same calc function?

By putting it there, and then using it from there:

Obj.prototype.calc = /* ...the function, use `this` rather than an argument... */;

and in Obj:

this.calc(); // No `return` needed

Simplified example:

var Foo = function(x, y) {
    this.x = x;
    this.y = y;
    this.calc();
};
Foo.prototype.calc = function() {
    this.z = this.x + this.y;
};

var f = new Foo(2, 3);
console.log(f.z); // "5"
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top