これは、ES5のObject.createのスタンドアロン実装です。
window.createObject = (function() {
var F = function () {};
return function(o) {
F.prototype = o;
return new F();
}
}());
およびその使用例:
var cat = createObject(animal);
animal
プライベート関数を呼び出そうとすると、の内部が少し乱雑になっていることに気づきました。例:
animal = (function() {
function privFunc(arg) {
this.property;
}
function publFunc(arg) {
privFunc.call(this, arg);
}
return {
publFunc: publFunc
}
}());
このタイプのパターンに従うためのよりクリーンな方法はありますか?特に、の必要性を排除しますprivFunc.call(this, arg)
。
別の方法は、同様に醜いです:
function privFunc(animal, arg) {
animal.property;
}
function publFunc(arg) {
privFunc(this, arg);
}