如何扩展 JavaScript 对象/函数以在初始化后公开 API 方法?

发布于 2024-11-01 02:44:41 字数 442 浏览 0 评论 0原文

我想创建一个像这样工作的 JavaScript 应用程序:

  • 有一个名为 bm 的函数/命名空间。
  • 一开始,bm 只是一个函数,它有一个名为 setup 的方法,因此有两种可能:调用 bm() 或定义一些调用 bm 的设置变量。设置(设置)
  • 要使用该库并公开 API,bm 必须首先通过调用以下函数进行初始化:bm(url, options)。如果初始化成功,API 应该会公开,因此 bm 现在有其他方法,例如 bm.method1bm.method2...

我不知道不知道这到底是如何可能的,所以我想听听任何想法、例子或建议。提前致谢。

I want to create a javascript application that works like this:

  • Has a function/namespace called bm.
  • At the start bm is just a function that has a method called setup, so two things are possible: calling bm() or defining some setup variables calling bm.setup(settings).
  • To use the library and expose the API bm has to be initialized first by calling the function: bm(url, options). If initialized succesfully, the API should be exposed so bm now has additional methods like bm.method1, bm.method2,...

I don't know how exactly is this possible, so I would like to hear any ideas, examples or suggestions. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由如风 2024-11-08 02:44:41

好处是函数是一流的对象,因此您可以向它们添加方法。然后,您的主实例创建函数只需要检查并确保其作为实例调用:

var bm = function(settings) {
    if (!(this instanceof bm)) {
        return new bm(settings);
    }

    // Now we are sure we are working with
    // a new instance. Let's do stuff here 
    // to our new object.
}

bm.setup = function(settings) {
    return new bm(settings);
}

可以通过以下任何方式调用:

var myObj = new bm();

var myObj = new bm(settings);

var myObj = bm();

var myObj = bm(settings);

var myObj = bm.setup(settings);

What's nice is functions are first-class objects, so you can add methods to them. Then your primary instance creating function just needs to check and make sure its called as an instance:

var bm = function(settings) {
    if (!(this instanceof bm)) {
        return new bm(settings);
    }

    // Now we are sure we are working with
    // a new instance. Let's do stuff here 
    // to our new object.
}

bm.setup = function(settings) {
    return new bm(settings);
}

This can be called any of these ways:

var myObj = new bm();

var myObj = new bm(settings);

var myObj = bm();

var myObj = bm(settings);

var myObj = bm.setup(settings);
无人问我粥可暖 2024-11-08 02:44:41

也许像...

function bp ( url, options ) {
    if ( !url || !options)
        return 'You fail mister';

    //declare stuff here:
    this.aMethod = function() {...};
    this.anAttribute = true;

    return this;
}
bp.setup = function( settings ) {
    return new bp( predefinedURL, settings);
}

Maybe something like...

function bp ( url, options ) {
    if ( !url || !options)
        return 'You fail mister';

    //declare stuff here:
    this.aMethod = function() {...};
    this.anAttribute = true;

    return this;
}
bp.setup = function( settings ) {
    return new bp( predefinedURL, settings);
}
紫竹語嫣☆ 2024-11-08 02:44:41

我在这里没有看到任何问题。您可能想向 bm() 添加一个回调函数,该函数在 API 初始化时调用,如下所示:

onAPIInitialized = function() { use the API };
bm(url, options, onAPIInitialized);

您可以像向普通对象添加函数一样向 bm 添加函数:

bm.method1 = function(...) {}

I don't see any problem here. You might want to add a callback function to the bm(), which is called when API is initialized, like this:

onAPIInitialized = function() { use the API };
bm(url, options, onAPIInitialized);

You can add functions to bm like to an ordinary object:

bm.method1 = function(...) {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文