Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
var Shape = function (size) {
this.size = size;
};
var CircleMixin = {
area: function () {
return Math.PI * this.radius * this.radius; // Мы не можем примешать CircleMixin к Shape ибо radius...
}
};
// VS
// ## Geometry.js
var Geometry = {
area: function (radius) {
return Math.PI * radius * radius;
}
};
// module.exports = Geometry;
// ## Shape.js
var Shape = function (size) {
this.size = size;
};
// module.exports = Shape;
// ## Circle.js
// var Shape = require('Shape'),
// Geometry = require('Geometry');
var Circle = function (size) {
this.size = size;
};
Circle.prototype = new Shape();
Circle.prototype.radius = function () {
return Geometry.radius(this.size); // явно передаем параметр
};
Array.prototype.slice.call(arguments,0);
alert(Math.max.apply(null, {length: 2, 0: 0, 1: 1})); // 1
(function(apply) {
'use strict';
try {
Function.apply(null, {length: 0});
}
catch (error) {
Function.prototype.apply = function(context, object)
{
if (Object.prototype.toString.call(object) !== '[object Array]')
object = Array.prototype.slice.call(object);
return apply.call(this, context, object);
}
}
}(Function.prototype.apply));
Class = function(){}
var a = new Class;
ECMAScript does not use classes such as those in C++, Smalltalk, or Java. Instead objects may be created in various ways including via a literal notation or via constructors which create objects and then execute code that initialises all or part of them by assigning initial values to their properties. Each constructor is a function that has a property named “prototype” that is used to implement prototype-based inheritance and shared properties.
да хотя бы child.prototype = parent.prototype
for(var i in parent.prototype){
if( {}.hasOwnProperty.call(parent, i) )
child.prototype[i] = parent.prototype[i];
}
// или, если есть jquery, $.extend(child.prototype, parent.prototype);
var object = {};
_.extend(object, Backbone.Events);
object.on("alert", function(msg) {
alert("Сработало " + msg);
});
object.trigger("alert", "событие")
var object = {};
object.events = new atom.Events(object);
object.events.add('alert', function(msg) {
alert("Сработало " + msg);
});
object.events.fire('alert', [ 'событие' ]);
Свежий взгляд на примеси в JavaScript