Mootools and the .__ proto__ object

Object.each, Object.keys... is not working properly, when I try to use an object that is a class attribute, check out this example :

var map = {a: 1, b: []}; // simple object
var SomeClass = new Class({'map': map}); // class attribute
var sc = new SomeClass();

With a simple object everything works fine

console.log(map.hasOwnProperty('a')); // true
console.log(map.hasOwnProperty('b')); // true
console.log(Object.keys(map)); // ['a', 'b']

But with sc.map does not work with scalar values ​​(int, boolean, string)

console.log(sc.map.hasOwnProperty('a')); // expected: true, returns: false
console.log(sc.map.hasOwnProperty('b')); // expected: true, returns: true
console.log(Object.keys(sc.map)); // expected: ['a', 'b'], returns: [b]

I understand that beacuse sc.map has a property __proto__

console.log(map.__proto__); // expected: empty Object
console.log(sc.map.__proto__); // expected: the "map" Object

I think this is a recent problem, because of this I have a huge pile of code, and some of them have stopped working. I do not want to change all my code to fix this problem, I assume that a patch is needed for mootools.

+4
source share
2 answers

. https://github.com/mootools/mootools-core/blob/master/Source/Class/Class.js#L85, : https://github.com/mootools/mootools-core/blob/master/Source/Core/Core.js#L348-L369

. .

var map = {a: 1, b: []}; // simple object
var SomeClass = new Class({'map': map}); // class attribute
var sc = new SomeClass();

console.log(sc.map.b.length);
map.b.push('doge');
console.log(sc.map.b.length); // 0
console.log(map.b.length); // 1
map.a = 'doge';
console.log(sc.map.a); // 1

, , copied in, . . , , - , .

. , , , . http://jsfiddle.net/mMURe/2/ - , . GH, . , . 1.3.2 mootools 1.4.5 . , , : https://github.com/mootools/mootools-core/issues/2544 - , ( )

, map , :

var map = {a:1},
    foo = new Class({
        initialize: function(){
            this.map = map;
        }
    });

var bar = new foo();
map.a++;
console.log(bar.map.a); // 2

, .

var map = {a: 1, b: []}; // simple object
var SomeClass = new Class(); // class attribute
// avoid the derefrence rush, go prototypal manually
SomeClass.prototype.map = map;

var instance = new SomeClass,
    other = new SomeClass;
console.log(instance.map.a); // 1
map.a++;
console.log(instance.map.a); // 2
// but also:
console.log(other.map.a); // 2

, MooTools - , , . javascript , .

+4

. , Object.clone(). Object.clone() , , __proto__, . :

var scMap = Object.clone(sc.map);
console.log(scMap.hasOwnProperty('a'));
console.log(scMap.hasOwnProperty('b'));
console.log(Object.keys(scMap));

, .

+1

All Articles