kitsonk / d2-proto Goto Github PK
View Code? Open in Web Editor NEWDojo 2.0 Prototypes and Experiments
License: Other
Dojo 2.0 Prototypes and Experiments
License: Other
In an attempt to adopt compose() in favor of declare() in ran into a number issues which would make the Dojo 1.x to 2.0 conversion a challenge. I do understand that compose() is not intended to be a 1-to-1 replacement for declare() but some enhancements would make the conversion much simpler.
The current implementation of compose is missing instanceof functionality which may not be a core requirement for the dojo toolkit itself but dijit (at least dijit tree) and many user applications rely on this functionality. IIHO it is a reasonable requirement to be able test if an object is an instance of some 'class' instead of having to test for the presence of specific object properties. For example:
function bingo() {
this.hasBalls = function () {
};
};
var myClass = compose(bingo);
var abc = new myClass();
if (abc.isInstanceOf(bingo) {
...
}
compared to:
var abc = new myClass();
if (abc.hasBalls !== undefined) {
...
}
or alternatively
if (hasBalls in abc) {
...
}
Considering Dojo 2.0 is embracing the ES5 object capabilities I was hoping to see that instances of objects declared with compose() would have their own properties. Consider the following example:
var myObject = {
bingo: function () {
console.log("Bingo");
},
name: "bingoClass"
}
var MyClass = compose(myObject);
var instance = new MyClass();
var properties = Object.getOwnPropertyNames(instance); // returns an empty array...
This approach (passing every property only by means of the prototype) prevent users from manipulating object properties using most, if not all, of the ES5 object functions. The only way to force 'class' objects to have their own properties is to define every 'class', used in a composition, as a function and declare every property using this as in:
function myObject() {
this.bingo = function () {
console.log("Bingo");
};
this.name = "bingoClass";
}
var MyClass = compose(myObject);
var instance = new MyClass();
var properties = Object.getOwnPropertyNames(instance); // returns an array with 'bingo' and 'name'
This however may have some nasty side-effects as every function will be called with the same set of arguments during 'class' instantiation which brings me to the next issue.
All functions used in a composition are called with the same set of arguments.
Consider the following example:
function Country(name, population) {
this.population = population;
this.country = name;
}
function Airport(name) {
this.airport = name;
}
function Capitol(name) {
this.capitol = name;
}
function EventTarget(parent) {
...
}
var myClass = compose(Country, Airport, Capitol, EventTarget);
var uk = new myClass("UK", "100m");
Unless all functions used in a composition have the same signature AND all parameters in that signature have exactly the same meaning to all functions it is not going to work. One would have to define every function signature with only one argument which would be a JavaScript key:value pairs object as in:
var myclass = function(keywordArgs) {
this.name = keywordArgs.name || "":
...
};
To make the behavior of 'classes' declared with compose predictable I would like to propose that only the very first function, that is arguments[0]
is called with the arguments list specified with the new
operator. All subsequent functions used in a composition() should be called without any arguments, thus making the first function the designated 'class' constructor.
function EventTarget(parent) {
this.addEventListener = function (...) {
};
if (arguments.length !== 0) {
...
}
}
var ctor = function(kwArgs) {
// Notice the use of isInstanceOf()
if (this.isInstanceOf(EventTarget)) {
EventTarget.call(this, kwArgs.parent);
...
this.name = kwArgs.name || "";
}
}
var myClass = compose(ctor, EventTarget, ... );
var instance = new myClass( {name: "Bingo", parent: someObject} );
This approach, (making the very first function, at arguments[0]
, the designated 'class' constructor), would mimic some of the dojo.declare constuctor property functionality:
var declare(null, {
constructor: function (keywordArgs) {
},
...
});
The compose() function signature would look like:
compose-function :== "compose" "(" [constructor / Object] ["," (Function / Object)]* ")"
constructor :== Function
Please let me know what you think.
One of the issues customers are having with dojo declarative format is its verbosity as compared to jquery mobile for example. What are your thoughts on reducing the amount of typing on things like:
data-dojo-type ==> data-type
And working with jq community to align on a common set of data- names to use, so that we can start to use similar syntax across these libs?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.