Javascript 调用类不带括号(jQuery 风格)

发布于 2024-12-11 03:35:28 字数 1210 浏览 0 评论 0 原文

我正在尝试建立像 jQuery 这样的库,只是为了学习目的。我让它工作得很好,并且我可以根据需要进行方法链接。我遇到的问题是能够使用类的括号调用方法:

我必须做什么:

$foo().get('id1');

我想要做什么:

$foo.get('id1');

这是当前的 javascript:

(function( window ) {
    var document = window.document;
    var fooTools = (function() {
        var fooTools = function( selector ) {
            return new fooTools.base.init( selector );
        };

        fooTools.base = fooTools.prototype = {
            init: function( selector ) {
                if( !selector ) {
                    return this;
                } 
                if( selector ) {
                    this[0] = selector;
                        this.length = 1;
                        return this;
                }
            },

            get: function( id ) {
                return document.getElementById( id );
            },

            //..other methods

        };       
        fooTools.base.init.prototype = fooTools.base;
        return fooTools;    
    }());
    window.fooTools = window.$foo = fooTools;
}( window ));

目前它工作得很好,但如果我不包括括号我收到一个错误,指出 .get() 方法不存在。我仍然想保持在其他方法中使用括号的能力,所以我只是希望它是可选的。谢谢!

I am trying to set up library like jQuery simply for learning purposes. I have it working decently well, and I can do method chaining as needed. The problem I am having is being able to call a method with the class' parenthesis:

What I have to do:

$foo().get('id1');

What I would like to do:

$foo.get('id1');

Here is the current javascript:

(function( window ) {
    var document = window.document;
    var fooTools = (function() {
        var fooTools = function( selector ) {
            return new fooTools.base.init( selector );
        };

        fooTools.base = fooTools.prototype = {
            init: function( selector ) {
                if( !selector ) {
                    return this;
                } 
                if( selector ) {
                    this[0] = selector;
                        this.length = 1;
                        return this;
                }
            },

            get: function( id ) {
                return document.getElementById( id );
            },

            //..other methods

        };       
        fooTools.base.init.prototype = fooTools.base;
        return fooTools;    
    }());
    window.fooTools = window.$foo = fooTools;
}( window ));

Currently it works just fine, but if I do not include the parenthesis I get an error that the .get() method does not exist. I still want to maintain the ability to use parenthesis for other methods, so i just want it be optional. Thanks!

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

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

发布评论

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

评论(3

苍景流年 2024-12-18 03:35:28

更改:

window.fooTools = window.$foo = fooTools;

为:

window.$foo = new fooTools();
window.fooTools = fooTools;

实时示例


编辑

window.fooTools = fooTools;
window.$foo= fooTools;
window.$foo.get = fooTools.prototype.get;

更新的示例

change:

window.fooTools = window.$foo = fooTools;

to:

window.$foo = new fooTools();
window.fooTools = fooTools;

Live Example


EDIT

window.fooTools = fooTools;
window.$foo= fooTools;
window.$foo.get = fooTools.prototype.get;

Updated Example

不可一世的女人 2024-12-18 03:35:28

定义两种不同类型的方法,就像我们在 jQuery 中所做的那样。

如果方法需要括号,例如 $foo('id1').attr({...}) 使用 $foo.prototype.method = function 定义它,否则,将其定义为 $foo $foo.get = function(){...} 的属性

编辑:这实际上是基于 IAbstract 的答案。

它将是 fooTools.prototype 而不是 $foo.prototype 因为 fooTools$foo 是实例的类的。

Define two different kind of methods, just like we do in jQuery.

If the method needs parenthesis such as $foo('id1').attr({...}) define it using $foo.prototype.method = function, else, define it as a property of $foo $foo.get = function(){...}

Edit: This is actually based on IAbstract's answer.

It would be fooTools.prototype not $foo.prototype since fooTools is the class that $foo is an instance of.

沙与沫 2024-12-18 03:35:28

然后你需要将 $foo 定义为函数以外的东西——现在你返回 fooTools,它是一个函数,所以你需要按顺序调用它访问它公开的内容。

(该死,他比我先一步。)

Then you need to define $foo as something other than a function--right now you're returning fooTools, which is a function, so you need to call it in order to access what it exposes.

(Drat, he beat me to it.)

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