jQuery.extend 和 jQuery.fn.extend 的区别? [英] Difference between jQuery.extend and jQuery.fn.extend?

查看:23
本文介绍了jQuery.extend 和 jQuery.fn.extend 的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解 jquery 插件语法,因为我想将两个插件合并到一.也需要能够停止 de interval 或运行多次的闪光灯.

I am trying to understand the jquery plugin syntax, because I want to merge two plugins into one. The blinker that also needs to be able to stop de interval or run a number of times.

无论如何,这个语法和

jQuery.fn.extend({
    everyTime: function(interval, label, fn, times) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, times);
        });
    },
    oneTime: function(interval, label, fn) {
        return this.each(function() {
            jQuery.timer.add(this, interval, label, fn, 1);
        });
    },

这个

$.fn.blink = function(options)
    {

因为看起来第一个(没有 =)是一种同时设置多个方法的方法.这是正确的吗?还有当我在这里的时候将元素和一些逻辑添加到 jquery 对象的原因是什么?

because it looks like the first(without =) is a way to set multiple methods at once. Is this right? Also while I am here What would be the reason to add the elements and some logic to the jquery object?

jQuery.extend({
    timer: {
        global: [],
        guid: 1,
        dataKey: "jQuery.timer",

(来自定时器插件)

推荐答案

jQuery.extend 用于扩展具有附加功能的任何对象,但 jQuery.fn.extend 用于扩展 jQuery.fn 对象,实际上增加了一次运行多个插件功能(而不是单独分配每个功能).

jQuery.extend is used to extend any object with additional functions, but jQuery.fn.extend is used to extend the jQuery.fn object, which in fact adds several plugin functions in one go (instead of assigning each function separately).

jQuery.extend:

var obj = { x: function() {} }

jQuery.extend(obj, { y: function() {} });

// now obj is an object with functions x and y

jQuery.fn.extend:

jQuery.fn.extend( {
        x: function() {},
        y: function() {}
});

// creates 2 plugin functions (x and y)

这篇关于jQuery.extend 和 jQuery.fn.extend 的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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