Le 13/10/2011 02:03, Allen Wirfs-Brock a écrit :
(...)
If you want it to also inherit from Array prototype you can define proto such 
as:

var proto = Array.prototype<| {/* additional "subclass" methods */};
Alex respond that new and instance of didn't work.
(using new to create DOM nodes is one of the characteristics of the Dart DOM 
design).

It took me three tries to get all the typos out, but I showed how new could be 
made to work:

var SubArray = Array<| function(...values) {
    return Object.getPrototypeOf(this)<| [...values];
}
I think that there is an ambiguity. (explained below)

/* add subclass methods */
SubArray.prototype.method1 = function() {...};
SubArray.prototype.method2 = function() {...};
/*or stache it:  SubArray.prototoype.{method1() {..}, method2() {}}; */

then you can say:

var s = new SubArray(1,2,3);
console.log(s.length);  //3
s[3] = 4;
console.log(s.length);  //4
console.log(Array.isArray(s)); //true
console.log(s instanceof SubArray); // true
So far so good

console.log(s instanceof Array); //true
i think that this should be false.
Object.getPrototypeOf(s) is the value of Object.getPrototypeOf(this) in the constructor which is the anonymous function 'prototype' property (fresh object inheriting from Object.prototype with the methods you added). My understanding of <| is that it sets the [[prototype]] property and nothing else. Consequently, I think that the semantics of "Array <| function(...){}" is to create a function with the prototype chain as follow: (anon function) --> Array --> Function.prototype --> Object.prototype --> null And unless otherwise specified, (anon function).prototype is a fresh object inheriting from Object.prototype (not Array.prototype)

But I think that the following should work:
-----
var SubArray = function(...values) {
   return Object.getPrototypeOf(this) <| [...values];
}
SubArray.prototype = Array.prototype <| {/*additional "subclass" methods*/};

var s = new SubArray(1,2,3);
// Array.isArray(s) === true, thanks to the "[...values]" syntax
// Object.getPrototypeOf(s) === SubArray.prototype
// SubArray.prototype is as follow:
// {additional methods} --> Array.prototype --> Object.prototype --> null
-----

However, as I said, the created object is an array thanks to the array syntax which doesn't apply to NodeLists or other DOM emulated constructs.


As I showed, applying <| to a function on the RHS may have (at least!) 2 semantics: 1) creating a function and only setting its prototype (as in "let f = EnhancedFunctionPrototype <| function () {}" in the wiki) 2) creating a constructor based on another constructor (which i think was your intention).

Maybe another operator should be introduced for the second case since there is currently no way in the language and the <| operator to distinguish how the LHS should be interpreted (as an object to set as [[Prototype]] or a constructor with some different semantics?)
The class+extends keywords could be this "operator".

David
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to