jQuery原型和构造函数链接 [英] jQuery prototype and constructor function chaining

查看:111
本文介绍了jQuery原型和构造函数链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery如何允许其构造函数充当接受参数的函数,同时它的构造函数还充当接受参数的函数?

How would jQuery allow its constructor to act as a function accepting arguments meanwhile its constructor also acts as a function accepting arguments?

我对JavaScript有点新鲜,如果这是一个菜鸟问题,请原谅我(我已经查看了源代码,但很难尝试剖析)。

I am a bit new to JavaScript, so excuse me if this is a noob question, (I have looked at the source but it's hard to try to dissect).

无论如何,作为示例 $(document).ready(< args>); 构造函数 $()和原型 ready()充当函数。怎么样?因为如果我试试这个:

Anyway, as an example $(document).ready(<args>); Both the constructor $() and the prototype ready() act as a function. How? Because if I try this:

var $ = function( selector ) {
    if(selector == document) {
        return document;
    }
};

$.prototype = {
    constructor: $,
    ready: function( args ) {
        if( isDomReady ) {
            args.apply( document );
        } else {
            window.onload = args;
        }
    }
};

var isDomReady = ( document.addEventListener || document.readyState == ("complete"|"loaded"|true|4) || document.onreadystatechange() ) ? true : false;

$(document).ready(function() { alert("Wibbles!") });

我收到错误 Uncaught TypeError:Object [object global]没有方法'ready'

推荐答案

这是一种称为函数链的编程技术。

It's a programming technique called function chaining.

你可以在这里看到更多:基本对象如何/ function链接在javascript中工作?

You can see more on this here: How does basic object/function chaining work in javascript?

这篇关于jQuery原型和构造函数链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆