【问题标题】:jQuery prototypejQuery 原型
【发布时间】:2012-10-27 00:22:09
【问题描述】:

根据这个 StackOverflow 答案What does jQuery.fn mean?jQuery.fn.jquery 中的 fn 属性是原型属性的别名。我假设这在这两种方法中是相同的,其完整代码如下

$.fn.map = function()$.fn.tweets = function()

那么我的问题是,例如,如果 $.fn.tweets 使用原型来创建 tweets 方法,那么带有$('tweets').tweets 的代码是否会调用它...

var $tweets = $('#tweets').tweets({
        query: buildQuery(approxLocation),
        template: '#tweet-template'
    });

如果是这样,它如何触发该方法。 例如,仅在文件加载时创建变量会触发该函数,该函数内部还有其他方法,即查询?感谢您的帮助

方法的完整代码

  $.fn.map = function(method) {
         console.trace();
         console.log(method);
        if (method == 'getInstance') {
            console.log("fn.map");
            return this.data('map');
        }

        return this.each(function() {
            var $this = $(this);
            var map = $this.data('map');

            if (map && MyMap.prototype[method]) {
                 map[method] (Array.prototype.slice.call( arguments, 1 ));
            } else if ( typeof method === 'object' || ! method ) {
                var options = method;
                $this.data('map', new MyMap( this, options ));
            } else {
                $.error( 'Method ' +  method + ' does not exist on jQuery.map' );
            }
        });
    }

   $.fn.tweets = function(method) {

        if ( methods[method] ) {
            return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {

            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.tweets' );
        }
    }

调用这些方法的变量?

 var $tweets = $('#tweets').tweets({
        query: buildQuery(approxLocation),
        template: '#tweet-template'
    });
var $map = $('#map').map({
    initialLocation: approxLocation,
    radius: 1000,
    locationChanged: function(location) {
        $tweets.tweets('setQuery', buildQuery(location));
    }
});

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    首先,原型只是对象。在这种情况下,是的:

    jQuery.prototype === jQuery.fn
    

    所以说jQuery.fn.map = function() {}就像说jQuery.prototype.map = function() {}

    当您使用$(selector | dom node | ...)实例化一个新的 jquery 对象时,您将返回一个自动继承所有原型方法的 jQuery 对象,包括 map、tweet 等。研究 Javascript 的原型继承模型和关于new,对象原型是如何工作的

    $ 只是对jQuery 的引用,它返回一个经过特殊修改的新对象$ 是一个返回新对象引用的函数。这是一个简化的例子(但你真的应该更多地研究原型继承,它已经被反复回答过很多次了):

    var A = function() {
    };
    
    A.prototype.doThing = function() {
    };
    
    var newObj = new A();
    
    newObj.doThing // this new object has this method because it's on A's prototype
    

    所以newObj.doThing 就像$(selector).tweet

    也可以随时read the source jQuery 并跟踪创建新对象时发生的情况。您可以在评论 // Define a local copy of jQuery 下看到在顶部附近发生的确切情况

    【讨论】:

    • 谢谢,那么在文件加载时创建这个变量会触发原型方法吗?在我提供的代码中,它不使用“新”构造函数。 var $tweets = $('#tweets').tweets({
    • @user1647484:然而,就在$ 函数内部,实际的jQuery 构造函数的一个实例是通过new 创建并返回的。看看this answer 的内部结构
    猜你喜欢
    • 2011-03-08
    • 2010-11-01
    • 2012-01-09
    • 2011-01-29
    • 1970-01-01
    • 2014-03-10
    • 2010-11-22
    • 2010-09-13
    • 1970-01-01
    相关资源
    最近更新 更多