$.extend() 与函数

发布于 2024-12-19 13:52:03 字数 596 浏览 1 评论 0原文

我正在编写一个 jQuery 插件,它需要执行回调函数,例如,当从应用插件的

我见过一些插件允许您在传递的选项中定义函数,如下所示:

$('div.foo').pluginBar({ 
    option1: 'red',
    someEvent: function() {
        // Do stuff
    }
});

我希望能够在我的插件代码中定义默认的 someEvent 函数,但如果用户定义了该函数它们传递的选项中的函数,它应该覆盖默认函数。这可以用 $.extend() 实现,还是我看错地方了?

我看过 插件创作 教程,但我认为它没有涵盖在任何地方扩展函数的默认行为。我已经阅读过有关使用 init: 函数的内容,但我宁愿能够定义一个函数(在插件命名空间内)并使用选项更改它传递给插件。

I'm writing a jQuery plugin that will need callback functions that are executed, for example, when an option is chosen from the <select> the plugin is applied to.

I've seen plugins that allow you to define functions in the options you pass it, like this:

$('div.foo').pluginBar({ 
    option1: 'red',
    someEvent: function() {
        // Do stuff
    }
});

I want to be able to define a default someEvent function in my plugin code, but if the user defines that function in the options they pass, it should overwrite the default function. Is this possible with $.extend(), or am I look in the wrong places?

I've had a look at the plugin authoring tutorial, but I don't think it covers extending the default behaviour of functions anywhere. I've read things about using an init: function, but I'd rather just be able to define a function (inside the plugin namespace) and change it with the options passed to the plugin.

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

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

发布评论

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

评论(3

情定在深秋 2024-12-26 13:52:03

下面是一个示例:

(function( $ ) {
  $.fn.pluginBar = function(options) {
      var settings = $.extend( {
          someEvent: function() {
              alert('default');
          }
      }, options);

      return this.each(function() {
          settings.someEvent();
      });
  };
})( jQuery );

$('div.foo').pluginBar({ 
    option1: 'red',
    someEvent: function() {
        alert('custom');
    }
});

如果您在连接插件时未指定 someEvent,则将使用默认的事件。

Here's an example:

(function( $ ) {
  $.fn.pluginBar = function(options) {
      var settings = $.extend( {
          someEvent: function() {
              alert('default');
          }
      }, options);

      return this.each(function() {
          settings.someEvent();
      });
  };
})( jQuery );

$('div.foo').pluginBar({ 
    option1: 'red',
    someEvent: function() {
        alert('custom');
    }
});

If you don't specify someEvent when wiring up the plugin, the default one will be used.

油焖大侠 2024-12-26 13:52:03

在 javascript 中,函数是第一类对象。这意味着,您可以像使用任何其他变量一样使用函数:

//making an anonymous function, and assigning it to a variable
var meep = function () {
    return 'meep';
};

//passing a function as a parameter
[ 'moop' ].map( meep ); //['meep']

//assigning it in an object literal
var weirdNoises = {
    'meep' : meep,

    'slarg' : function () {
        return 'slarg';
    }
};

weirdNoises.meep(); //'meep'
weirdNoises.slarg(); //'slarg'

//you can also return functions
function meeper () {
    return meep;
}
function slarger () {
    return function () {
        return 'slarg';
    };
}

//meeper returns a function, so the second () means "execute the function
// that was returned"
meeper()(); //'meep'
slarger()(); //'slarg'

如您所见,函数就像任何其他值一样。因此,您可以定义一个默认选项,该选项将是一个函数,并像其他任何东西一样覆盖它。

$.fn.weirdNoise = function ( options ) {
    var defaults = {
        makeNoise : function () {
            return 'Rabadabadaba';
        },
        isSilly : true
    };

    return $.extend( defaults, options );
};

var raba = $( 'foobar' ).weirdNoise().makeNoise();
raba.makeNoise(); //'Rabadabadaba'
raba.isSilly; //true

var shaba = $( 'foobar' ).wierdNoise({
    makeNoise : function () {
        return 'Shabadabadoo';
    }
});
shaba.makeNoise(); //'Shabadabadoo'
shaba.isSilly; //true

这是一个人为的例子,但我认为它说明了这一点。

In javascript, functions are first-class objects. Meaning, you can use functions like you would any other variable:

//making an anonymous function, and assigning it to a variable
var meep = function () {
    return 'meep';
};

//passing a function as a parameter
[ 'moop' ].map( meep ); //['meep']

//assigning it in an object literal
var weirdNoises = {
    'meep' : meep,

    'slarg' : function () {
        return 'slarg';
    }
};

weirdNoises.meep(); //'meep'
weirdNoises.slarg(); //'slarg'

//you can also return functions
function meeper () {
    return meep;
}
function slarger () {
    return function () {
        return 'slarg';
    };
}

//meeper returns a function, so the second () means "execute the function
// that was returned"
meeper()(); //'meep'
slarger()(); //'slarg'

As you can see, functions are just like any other value. So, you can define a default option that'll be a function, and override it like anything else.

$.fn.weirdNoise = function ( options ) {
    var defaults = {
        makeNoise : function () {
            return 'Rabadabadaba';
        },
        isSilly : true
    };

    return $.extend( defaults, options );
};

var raba = $( 'foobar' ).weirdNoise().makeNoise();
raba.makeNoise(); //'Rabadabadaba'
raba.isSilly; //true

var shaba = $( 'foobar' ).wierdNoise({
    makeNoise : function () {
        return 'Shabadabadoo';
    }
});
shaba.makeNoise(); //'Shabadabadoo'
shaba.isSilly; //true

A contrived example, but I think it illustrates the point.

染火枫林 2024-12-26 13:52:03

是的,您可以扩展具有函数值的对象。

var originalFunction = function() { 
    alert( 'original' );
}

var a = { 
    foo: 'bar', 
    func: originalFunction 
};

var b = { 
    foo: 'baz', 
    func: function() { 
        alert( 'ok' ); 
    } 
};

var c = $.extend( {}, a, b );

c.func();    // alerts 'ok'

在 JavaScript 中,函数和其他值之间通常没有实际区别,因此您可以像对待其他对象或数据类型一样对待函数。

Yes, you can extend objects that have function values.

var originalFunction = function() { 
    alert( 'original' );
}

var a = { 
    foo: 'bar', 
    func: originalFunction 
};

var b = { 
    foo: 'baz', 
    func: function() { 
        alert( 'ok' ); 
    } 
};

var c = $.extend( {}, a, b );

c.func();    // alerts 'ok'

In JavaScript there's often no practical difference between functions and other values, so you can treat functions as you would other objects or data types.

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