関数コンストラクターがあります。どの関数 (オブジェクト) が呼び出せるかを制御したい。例を次に示します。
function Bar() {
// foo can be created only here, when Bar is instantiated
var foo = new Foo();
}
function Foo() {
// I'd like to have something like this here:
if (caller != Bar) {
alert("Not allowed, the caller is not Bar");
return;
}
}
var bar = new Bar(); // this is correct, Foo can be created inside Bar
var foo = new Foo(); // prints "Not allowed, the caller is not Bar" and exits
JSで実装することは可能ですか?そのような種類の制御のためのいくつかの機能はありますか?
このように作成が中止された場合、Foo から何が作成されますか?