Mootools and object.__proto__ - mootools

Object.each, Object.keys, ... doesn't work fine when I try to use with an object that is a class attribute, check 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 ok
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 realize it is beacuse sc.map has a __proto__ property
console.log(map.__proto__); // expected: empty Object
console.log(sc.map.__proto__); // expected: the "map" Object
I think it is a recent issue, because I have a huge bunch of code and some things stopped working because of this. I don't want to change all my code to fix this problem, i guess some patch for mootools is needed.

wow. so source code, https://github.com/mootools/mootools-core/blob/master/Source/Class/Class.js#L85, much merge: https://github.com/mootools/mootools-core/blob/master/Source/Core/Core.js#L348-L369
want derefrence. excellent.
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
in seriousness, not a recent change at all, any non-primitives are being copied in, don't panic. it is a good thing. You probably don't want a prototype for your object that can change because of something it references outside.
now. that being said, it's weird where a ends up from. http://jsfiddle.net/mMURe/2/ - I agree this is not ideal and is somewhat unexpected. filing an issue on GH, though don't hold your breath. if it's a recent change, it will be related to browser changes. Same code breaks in 1.3.2 and mootools 1.4.5 is quite old now and unchanged. The issue I filed on your behalf is here: https://github.com/mootools/mootools-core/issues/2544 - feel free to elaborate and add more usecases of where this is actually useful (I still don't like passing objects into prototypes)
But if you need to have your object have the map object by reference and avoid the merge, you can just put the reference in your constructor method instead:
var map = {a:1},
foo = new Class({
initialize: function(){
this.map = map;
}
});
var bar = new foo();
map.a++;
console.log(bar.map.a); // 2
but if you absolutely must break the norm and know the side effects, then you can.
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
remember, MooTools Class is just constructor creation sugar that takes the pain away from having to do repetitive and non-terse actions. it still is javascript under the hood and you can use classic prototypal patterns on top.

I just encountered this issue as well. To avoid it I used Object.clone(). Object.clone() returns a normal object rather than a class object, so it does not have the __proto__ property, and everything should run fine. In the example you gave above, I would do this:
var scMap = Object.clone(sc.map);
console.log(scMap.hasOwnProperty('a'));
console.log(scMap.hasOwnProperty('b'));
console.log(Object.keys(scMap));
Hope this helps.

Related

An attempt at understanding what ECMAScript-6 Function.prototype.bind() actually does

This question is a follow-up to this one. For some reason I'm coming back to JS after 7 years and boy, I can hardly recognize the dear old beast.
Purely for educational purpose, I decided to rewrite naive implementations of the various things Function.prototype.bind() allows to do. It's just an exercise to try to understand what's going on and spark a few questions.
I would be happy to stand corrected for all the mistakes and misunderstandings in my examples and comments.
Also, this code is not mine. I got the idea from a blog and only slightly tweaked it, but unfortunately I lost track of the source. If anyone recognizes the original, I'll be happy to give due credit. Meanwhile, I apologize for the blunder.
Naive binding
The initial idea is simply to do what lambda calculus savvies apparently call a "partial application", i.e. fixing the value of the first parameters of a function, that also accepts an implicit "this" first parameter, like so:
Function.prototype.naive_bind = function (fixed_this, ...fixed_args) {
const fun = this; // close on the fixed args and the bound function
return function(...free_args) { // leave the rest of the parameters free
return fun.call(fixed_this, ...fixed_args, ...free_args);
}
}
Binding constructors (1st round)
class class1 {
constructor (p1, p2) {
this.p1 = p1;
this.p2 = p2;
}
}
var innocent_bystander = { "huh?":"what?" }
var class2 = class1.naive_bind(innocent_bystander);
class2 (1,2) // exception: class constructor must be invoked with new (as expected)
console.log(new class2(1,2)) // exception: class constructor must be invoked with new (Rats!)
function pseudoclass1 (p1, p2) {
this.p1 = p1;
this.p2 = p2;
}
var pseudoclass2 = pseudoclass1.naive_bind(innocent_bystander);
pseudoclass2 (1,2) // same exception (as expected)
console.log(new pseudoclass2(1,2)) // same again (at least it's consistent...)
Tonnerre de Brest ! Apparently the runtime is not happy with a mere wrapper based on Function.prototype.call().
It seems the real bind() is adding the dollop of secret sauce needed to give the generated function the appropriate "constructor" flavour (Apparently by "constructor" the ECMA 262 specification does not mean a class constructor, but any function that can be invoked with "new" and use "this" to populate the properties and methods of a freshly created object)
Binding other functions
var purefunction1 = console.log
var purefunction2 = purefunction1.naive_bind (null, "Sure,", "but")
purefunction2 ("you can still", "bind pure", "functions")
// sure, but you can still bind pure functions
// ...and make animals talk (by binding simple methods)
var cat = { speech: "meow" }
var dog = { speech: "woof" }
var fish= { speech: "-" }
var talk = function(count) { console.log ((this.speech+", ").repeat(count-1) + this.speech + "!") }
talk.naive_bind (cat,1)(); // meow!
talk.naive_bind (dog,1)(); // woof!
talk.naive_bind (cat)(3) // meow, meow, meow!
talk.naive_bind (fish)(10) // -, -, -, -, -, -, -, -, -, -!
// and bind arrow functions
// ("this" is wasted on them, but their parameters can still be bound)
var surprise = (a,b,c) => console.log (this.surprise, a,b,c)
var puzzlement = surprise.naive_bind(innocent_bystander, "don't", "worry");
// "this" refers to window, so we get our own function as first output.
surprise ("where", "am", "I?") // function surprise(a, b, c) where am I?
// the bound value of this is ignored, but the other arguments are working fine
puzzlement("dude") // function surprise(a, b, c) don't worry dude
Apparently, everything works as expected. Or did I miss something?
Binding constructors (2nd round)
We apparently can't get away with passing a wrapper to new, but we can try invoking new directly.
Since the value of this is provided by new, the construction-specialized wrapper has only to worry about real constructor parameters.
Function.prototype.constructor_bind = function (...fixed_args) {
const fun = this;
return function(...free_args) {
return new fun (...fixed_args, ...free_args);
}
}
var class1_ctor = class1.constructor_bind(1);
console.log (class1_ctor(2)) // class1 { p1:1, p2:2 }
var monster = (a,b) => console.log ("boooh", a, b)
var abomination = monster.constructor_bind(1);
console.log (abomination(2)) // exception: fun is not a constructor (as expected)
Well, that seems to cut it. I imagine the real bind() is far safer and faster, but at least we can reproduce the basic functionalities, namely:
providing a fixed value of this to methods
doing partial applications on any legit function, though constructors require a specific variant
edit: questions removed to comply with SO policy.
Replaced by just one, the one that matches the title of this post, and that I tried to explore by providing a naive and possibly erroneous equivalent of what I thought the real function did:
Please help me understand how the native Function.prototype.bind() method works, according to the version 6.0 of the ECMAScript specification.
The only bit you have been missing is the introduction of new.target in ES6, which a) makes it possible to distinguish between [[call]] and [[construct]] in a function and b) needs to be forwarded in the new call.
So a more complete polyfill might look like this:
Function.prototype.bind = function (fixed_this, ...fixed_args) {
const fun = this;
return function(...free_args) {
return new.target != null
? Reflect.construct(fun, [...fixed_args, ...free_args], new.target)
: fun.call(fixed_this, ...fixed_args, ...free_args);
}
}
Some other details would involve that fun is asserted to be a function object, and that the returned bound function has a special .name, an accurate .length, and no .prototype. You can find these things in the spec, which apparently you've been reading already.

cocos2d-js: b2DebugDraw can't be initialized. element.getContext("2d") returns null

I tried to use b2DebugDraw and I couldn't initialize it, because I get a null returned from getContext function. I know that this is not a cocos2d element, but it works fine in other contexts.
I created a new empty hello world project with "cocos new -l js", and placed following code at the end of the ctor function of the HelloWorldLayer:
var b2Vec2 = Box2D.Common.Math.b2Vec2,
b2World = Box2D.Dynamics.b2World,
b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
this.world = new b2World(new b2Vec2(0, -5), true);
var debugDraw = new b2DebugDraw();
var element = document.getElementById("gameCanvas"),
context = element.getContext("2d"); // context is null so
debugDraw.SetSprite(context); // sprite is not set
debugDraw.SetDrawScale(20.0);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0); // and crashes in SetLineThickness
What is wrong?
Engine version is 3.0a2
I'm trying to pass from using box2d + ease.js to cocos2d v3.0 with box2d and it is giving me the same troubles;
I finally solved adding a more canvas to the main "gameCanvas" (i put it before it in my index.html) and calling it in my b2DebugDraw method in place of the cocos2d related one:
draw:function (ctx) {
this.world.DrawDebugData();
this._super(ctx);
},
debugDraw: function(){
var myDebugDraw = new b2DebugDraw();
myDebugDraw.SetSprite(document.getElementById("debug").getContext("2d"));
//debug instead of gameCanvas
myDebugDraw.SetDrawScale(scale);
myDebugDraw.SetFillAlpha(0.8);
myDebugDraw.SetLineThickness(1.0); // no need to change this anymore
myDebugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
this.world.SetDebugDraw(myDebugDraw);
}
nothing more than this. No need either to change the SetLineThickness, the error was probably related to the null context.
Hope this helps

ActionScript:ArrayCollection declaration

I am new to ActionScript and Flex.I need to learn the application developed using Flex and ActionScript.I saw an ArrayCollection declaration :
[Bindable]
public var someThing:ArrayCollection = new ArrayCollection([[]]);
What does that declaration mean.Is it multi dimensional?What is it?
This would be an ArrayCollection which is a wrapper for an Array basically to give you sort and filter function capabilities (and other methods). The constructor optionally takes an Array as an argument, [] is a new array with no elements. [[]] is an array with one element in it that is an array with no elements in it. So this is saying make a new ArrayCollection with it's source as an array which in turn contains one element that is an empty array. Not sure why they would be doing this, but that's what it does.
To sum up, you can declare and instantiate an array in AS3 like:
var myArray:Array = [];
or
var myArray:Array = new Array();
To add since this is I believe ECMAScript specific as well you can instantiate an object similarly using {}. Such as:
var myObj:Object = {};
or
var myObj:Object = new Object();
Either of these sites is good reference material for AS3:
http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee5.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee1
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html
When I first got started with Flex (having a background in HTML/JS, C, C++, and Java) I found these videos to be really useful (if not always 100% correct/up to date, they explain the overarching concepts):
http://www.adobe.com/devnet/flex/videotraining.html

How to clone an object without knowing the exact type in AIR for iOS

I am writing an iOS game in Flash and I need a way to clone polymorphic objects.
I have BaseClass, SubClass1, SubClass2 (and so on...) and I need a clone() method in BaseClass, that will create a copy of the current object, without a conditional such as
var obj:BaseClass;
if(this is SubClass1) {
obj = new SubClass1();
}else if(this is SubClass2) {
obj = new SubClass2();
}else...
I need a way to create an object and create the exact bytes (yes, a shallow copy is enough for my purpose) of the object. I've looked at:
AS3 - Clone an object
As3 Copy object
http://actionscripthowto.com/how-to-clone-objects-in-as3/
But none seem to work. Probably not available in AIR 3.3 for iOS SDK. (they compile, but the code doesn't work in my case)
Is there any other way, or did anybody achieve to clone an object in AIR for iOS?
Thanks,
Can.
Bit-by-bit cloning cannot be done with ActionScript, unless your class only contains primitive values (i.e. a simple data structure). That's what the ByteArray approach you've linked to in this question's answer is used for - but when you're dealing with complex types, especially display objects, you'll soon come to the limits (as, I gather, you have already realized).
So this more or less leaves you with two options:
Create a new object and copy all of its fields and properties.
This is the way to go if you're going to need behavior and field values, and you didn't use any drawing methods (i.e., you can not copy vector graphics this way). Creating a new class instance without knowing its exact type can be done in a generalized way using reflections, getQualifiedClassName() and getDefinitionByName() will help you there, and if you need more than just the name, describeType(). This does have limits, too, though:private fields will not be available (they don't appear in the information provided by describeType()), and in order to not run into performance problems, you will have to use some sort of cacheing. Luckily, as3commons-reflect has already solved this, so implementing the rest of what you need for a fully functional shallow copy mechanism is not too complex.
Create a new instance like this:
var newObject:* = new Type.forInstance( myObject ).clazz();
Then iterate over all accessors, variables and dynamic properties and assign the old instance's values.
I have implemented a method like this myself, for an open source framework I am working on. You can download or fork it at github. There isn't any documentation yet, but its use is as simple as writing:
var myCopy:* = shallowCopy( myObject );
I also have a copy() method there, which creates a true deep copy. This, however, has not been tested with anything but data structures (albeit large ones), so use at your own risk ;)
Create a bitmap copy.
If you do have vector graphics in place, this is often easier than recreating an image: Simply draw the content of the object's graphics to a new Bitmap.
function bitmapCopy( source:Sprite ):Bitmap {
source.cacheAsBitmap = true;
var bitmapData:BitmapData = new BitmapData( source.width, source.height, true, 0xFFFFFF );
bitmapData.draw( source, new Matrix(), null, null, null, true );
return new Bitmap( bitmapData, PixelSnapping.AUTO, true );
}
You need to create an abstract clone method in the base class and implement it for each subclass. In the specific implementations, you would copy all of the properties of the object to the new one.
public class BaseClass {
public function clone():BaseClass
{
// throw an error so you quickly see the places where you forgot to override it
throw new Error("clone() should be overridden in subclasses!");
return null;
}
}
public class Subclass1 extends BaseClass {
public override function clone():BaseClass
{
var copy:Subclass1 = new Subclass1();
copy.prop1 = prop1;
copy.prop2 = prop2;
// .. etc
return copy;
}
}
If you wanted to create a generic default implementation of clone, you could use describeType to access the properties and copy them over:
public function clone():BaseClass
{
var defn:XML = describeType(this);
var clsName:String = defn.#name;
var cls:Class = getDefinitionByName(clsName) as Class;
var inst:* = new cls();
for each(var prop:String in (defn.variable + defn.accessor.(#access == 'readwrite')).#name )
{
inst[prop] = this[prop];
}
return inst;
}
The main issue with this is that the describeType XML can get quite large - especially if you are dealing with objects that extend DisplayObject. That could use a lot of memory and be slow on iOS.

AS3 - nulling elements within a Vector structure

I'm using a Vector structure in ActionScript 3 to store references to a custom class. My Vector is fixed length for performance reasons, and also because it needs to map 1:1 to another data structure.
I'm trying to figure out how I "remove" an element from this fixed list. I don't want the length of the Vector to change, I just want the value of that element to be "null". Or more specifically, when I test for the truth of that element (eg: if (myVector[index]) { // do something... }), I want that test to fail for that element, since it's "empty".
So naturally, I've tried:
myVector[index] = null;
but that throws a TypeError, as does
myVector[index] = Vector.<MyClass>([]);
So what's the process for "emptying" an element of a Vector?
There must be something wrong with your index variable because
myVector[0] = null;
does not throw a TypeError when I try that. Is index an int or uint?
if you're receiving:
TypeError: Error #1009: Cannot access
a property or method of a null object
reference
than you probably haven't added your class to the object you are trying to manage (IE: you haven't added your Display objects to the stage that you are trying to nullify)
posting more code could help, but something like the following should work for you:
var spriteA:Sprite = new Sprite();
var spriteB:Sprite = new Sprite();
var spriteC:Sprite = new Sprite();
var spritesVector:Vector.<Sprite> = new <Sprite>[spriteA, spriteB, spriteC];
spritesVector.fixed = true;
trace(spritesVector); //[object Sprite],[object Sprite],[object Sprite]
spritesVector[1] = null;
trace(spritesVector); //[object Sprite],null,[object Sprite]