Edit:
@Tomalak - JavaScript:
var flowers = new Misdirection;
flowers.abracadabra();
alert(flowers);
function Misdirection() {
this.abracadabra = function () {
this = new Rabbit;
};
}
function Rabbit() {
this.toString = function () {
return "Eh... What up, doc?";
};
}
ReferenceError: Cannot assign to 'this'. ; this, - this.
, this flowers. misdirection . .
, Function.call, Function.apply Array.map. , , this. , ( ), , abracadabra Misdirection flowers() flowers.abracadabra().
:
JavaScript - . , , :
var flowers = new Misdirection(&flowers);
flowers.abracadabra();
alert(flowers);
function Misdirection(flowers) {
this.abracadabra = function () {
*flowers = new Rabbit;
};
}
function Rabbit() {
this.toString = function () {
return "Eh... What up, doc?";
};
}
, JavaScript , :
var Square = new Class(function (ctor, uber) {
*ctor = constructor;
var side;
function constructor(length) {
side = length;
}
this.area = function () {
return side * side;
};
return &uber;
});
var Cube = new Class(function (ctor, uber) {
*ctor = constructor;
function constructor(side) {
uber(side);
}
this.area = function () {
return 6 * uber.area();
};
return &uber;
}, Square);
var cube = new Cube(5);
alert(cube.area());
function Class(claus, Uber) {
Claus.__proto__ = Uber === void 0 ? Class.prototype : Uber;
return Claus;
function Claus() {
var self = this;
var called;
var uber = Uber === void 0 ? function () {
throw new Error("No uber class specified.");
} : function () {
if (!called) {
called = "Cannot call uber class constructor more than once.";
var args = Array.prototype.slice.call(arguments);
args = Array.prototype.concat.call([null], args);
var base = new (Function.prototype.bind.apply(Uber, args));
self.__proto__.__proto__ = base;
self.__proto__.__proto__.constructor = Claus;
*uber = base;
} else throw new Error(called);
};
var constructor = new Function;
uber = claus.call(this, &constructor, uber);
constructor.apply(this, arguments);
};
}
, , , . , , - JavaScript. , ( ):
class Square {
var side;
function constructor(length) {
side = length;
}
this.area = function () {
return side * side;
};
}
class Cube extends Square {
function constructor(side) {
uber(side);
}
this.area = function () {
return 6 * uber.area();
};
}
var cube = new Cube(5);
alert(cube.area());
, *ctor = constructor; return &uber; . , . , Class , .
The beauty of simulating pointers in JavaScript is demonstrated in the above program, where the variable uberin the class is Cubeinitially the constructor of the base class. However, when he called it replaced with this instance of the base class, which becomes a prototype this.
It also means that the instance Cubewill not be an instance Squareif and until the constructor of the uber class is called from Cube.