My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Thursday, October 13, 2011

about JS VS VBScript VS Dart

You can take it as a joke since this is not a proper comparison of these web programming languages.

JS Dart VBScript
types √ √ sort of
getters and setters √ √ √
immutable objects √ √ √
prototypal inheritance √
simulated classes √
"real" classes √ √
closures √ √
weakly bound to JS √ √
obtrusive for JS (global) may be √ √
obtrusive for JS (prototypes) may be √
operator overload √
cross browser √
real benefits for the Web √ ? ?

If you are wondering about JS types, we have both weak type and strong type, e.g. Float32Array.
When StructType and ArrayType will be in place then JS will support any sort of type.

2 comments:

make facebook applications said...

WHAT is the difference between both ?
i think JS is the best script.

glathoud said...

Concerning "real" classes: Who's not happy with with prototype inheritance?

It might just be sufficient to add a small thing to ES5 that calls the various implementations of an object's method along its prototype chain, in an efficient manner (built-in).

So, along those lines (sorry for the length of my comment...):

Object.callTopDown = function ( /*object*/o, /*string*/m /*, ... args ...*/ ) {
Object.applyTopDown( o, m, Array.prototype.slice.call( arguments, 2 ) );
}

Object.applyTopDown = function ( /*object*/o, /*string*/m, /*array*/args ) {
// Backtrack the prototype chain of `o` to list all
// implementations of method `m`.
//
// (A built-in version could be much more efficient.)

var arr = []
, prev_impl
, impl
;
for (var o2 = o;
o2;
o2 = Object.getPrototypeOf( o2 ) // ECMAScript 5
)
{
impl = o2[ m ];
if (impl && impl !== prev_impl)
arr.push( prev_impl = impl );
}

if (!arr.length) throw new Error('Method "' + m + '" not found');

// Apply all implementations of method `m`, from the top to the
// bottom of the prototype chain of `o`.

while (impl = arr.pop())
impl.apply( o, args );
}

function test() {

var acc = [];

// Setup a prototype chain

function CA() {}
CA.prototype.doit = function (x,y,z) { acc.push('CA.doit',x,y,z); console.log('CA.doit',x,y,z); };

function CB() {}
CB.prototype = new CA();
// Not overriding here.

function CC() {}
CC.prototype = new CB();
CC.prototype.doit = function (x,y,z) { acc.push('CC.doit',x,y,z); console.log('CC.doit',x,y,z); };

function CD() {}
CD.prototype = new CC();
CD.prototype.doit = function (x,y,z) { acc.push('CD.doit',x,y,z); console.log('CD.doit',x,y,z); };

// Define truth

var expected_acc = [ 'CA.doit', 1, 2, 3, 'CC.doit', 1, 2, 3, 'CD.doit', 1, 2, 3 ];

// Run

d = new CD();
Object.callTopDown( d, 'doit', 1, 2, 3 );

// Check

console.assert( expected_acc.length === acc.length, 'wrong acc.length' );
expected_acc.forEach( checkOne );

function checkOne(x, i) {
console.assert( expected_acc[ i ] === acc[ i ] , 'wrong acc['+i+']:' , acc[i] );
}
}