我有函数构造函数。我想控制什么函数(对象)可以调用它。这是示例:
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 创建什么?