JS extension or jquery extension notation

Source: Internet
Author: User

<script>
JS extension string function test, other analogy
String.prototype.test = function (s) {
alert (this+s);
}
var str = ' Hello ';
Str.test (' World ');//helloworld

JQ extension
(function ($) {
$.fn.test = function (OP) {
var defaults = {A: ' No '}
var setings = $.extend (DEFAULTS,OP);
alert (SETINGS.A);
}
}) (JQuery);

Call
$ (function () {
$ (). Test ();
$ (). Test ({A: ' yes '});
})
</script>

JS extension Method: 1 Define class static method extension 2 Define Class object method extension
var aclass = function () {}


1 defining static methods for this class
Aclass.sayhello = function () {
Alert (' Say hello ');
}


2 Defining object methods for this class object
AClass.prototype.protoSayHello = function () {
Alert (' prototype say hello ');
}




Aclass.sayhello (); static method of//aclass


var aobject = new AClass ();
Aobject.protosayhello (); Method extensions for Class objects


Method Extensions for jquery


Defining extension methods for jquery
1 static methods for defining jquery
Jquery.extend ({
Statichello:function () {
Alert ("WWG, Statichello");
}
});


var str = $.statichello ();


2 Defining extension methods for jquery objects
JQuery.fn.ObjHello = function () {
Return alert ("Objhello");
}


$ ("#htmlDiv"). Objhello ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.