Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
var a = Array.new(1, 2, 3); console.log(a[-1]); // 3 console.log(a[-2]); // 2 a[-1] = 10; console.log(a); // 1,2,10 console.log(a[-1]); // 10
var foo = Object.new({
__get__: function (name) {
console.log('__get__:' + name);
}
});
foo.bar; // __get__: bar
foo.baz; // __get__: baz
foo.__get__ = function (name) {
console.log("New get: " + name);
};
foo.bar; // New get: barvar foo = Object.new({
// normal object level
data: {
x: {
value: 10,
writable: true
}
},
// meta-level
meta: {
noSuchProperty: function fooNoSuchProperty(name) {
console.log("noSuchProperty: " + name);
},
noSuchMethod: function fooNoSuchMethod(name, args) {
console.log("noSuchMethod: " + name + ", args: " + args);
}
},
// a prototype of an object
proto: Object.prototype
});
foo.bar; // noSuchProperty "bar"
foo.bar(1, 2, 3); // noSuchProperty: "bar" -> noSuchMethod: "bar", args: 1, 2, 3
// normal-level doesn't disturb meta-level
foo.noSuchProperty = 10; // assign to normal-level, but not to meta-level
// still noSuchProperty of the meta-level is called
foo.baz; // noSuchProperty: "baz"
// the same with noSuchMethod
foo.noSuchMethod = 20; // assign to normal-level, but not to meta-level
// still the meta-level noSuchMethod is activated
foo.baz(10, 20, 30); // noSuchMethod: "baz", args: 10, 20, 30.. — это расширение Mozilla, как вы уже написали, поэтому ждать чего-то ( __descendants__) стоит только от разработчика расширения. Прочие браузеры реагируют на .. как на ошибку вроде «expected identifier, got '.'» т.к. парсят код «по стандарту» и врятли будут что-то переделывать.Второе, функции-прокси можно использовать для создания псевдо-классов, сущности которых функции(instances are callable).
function Thing() { /* initialize state, etc */ return makeCallable(this, function() { /* actions to perform when instance is called like a function */ }); }
var $table = $('table');
$table('td');
// вместо
$table.find('td');
var Nothing = function () { return Nothing; }
var NewNothing = new new new new new Nothing();
console.log(NewNothing == Nothing); // true
a = something(); // если метод вываливается с ошибкой, то возвращается Nothing
console.log(a.split(1, 15).length ) // a любые последующие манипуляции c медотами/полями возвращают Nothing
a[1] вместо a[a.length - 1].
ES5 Harmony Proxy — меняем семантику JavaScript внутри самого JavaScript