开发者

Build js function within jQuery SDK

i've created a new js function like this

Object.prototype.testFn = function(){
    s=this;
    alert(s);
    }
a='word';           
a.testFn(); 

i'ts worki开发者_运维问答ng as well, but, when i use jQuery sdk, then i call jQuery live, my testFn function is called repeatedly. and sometimes, the whole script is not working...


As said in my comment, extending Object.prototype is a bad idea. It most likely will break other libraries.

There is no special way to define functions when you are using jQuery. It is simply:

function myFunction(obj) {
    // manipulate obj
}

But as you mention jQuery, mayby you want to create a plugin, the basic structure is:

jQuery.fn.myPlugin = function() {

  // Do your awesome plugin stuff here

};

You have to extend jQuery.fn.

How to create a plugin is very well explained in the Plugins/Authoring tutorial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜