jquery Source Code Analysis: The jquery tool method detailed 2

Source: Internet
Author: User

jquery's Tool method, in fact, is static method, the source code is through the Extend method, these tools are added to the jquery constructor.

Jquery.extend ({

......

Type:function (obj) { //$.type (), Judging type
if (obj = = null) { //null,undefined
return String (obj); //return null,undefined string
} //core_tostring = {}.tostring
return typeof obj = = = "Object" | | typeof obj = = = "function"?
class2type[Core_tostring.call (obj)] | | "Object": //number,string and other packaging types, typeof judgment is an object, so enter into the ToString judgment, and eventually return number,string

typeof obj; //basic type, number,string directly with TypeOf.
},

Isplainobject:function (obj) { //To determine if an object argument is, {},new
if (Jquery.type (obj)!== "Object" | | obj.nodetype | | jquery.iswindow (obj)) {
return false; //dom nodes, window, and so on are objects, but not object arguments.
}

try { //window.location is Object,core_hasown is {}.hasownproperty (own property, not a prototype property)
if (Obj.constructor &&!core_hasown.call (Obj.constructor.prototype, "isprototypeof")) {
        isPrototypeOf only exists in Object.prototype object, Core_hasown.call (Obj.constructor.prototype, "isprototypeof") The meaning of this sentence is: Obj.constructor.prototype whether there is isprototypeof attribute, from the above, only object.prototype have, so only object.prototype return true. For example, I pass in a person (constructor) whose prototype has no isprototypeof property, so it returns false. Person is not an object argument.

return false;
}//In Firefox version 20, if the operation Window.location.constructor multiple times, will be thrown wrong
} catch (e) {
return false;
}

return true;
},

Isemptyobject:function (obj) { //{},[], which Returns True if there are no properties and methods under the object itself. function person () {}; var person = new Person (), returns True

var name;
for (name in obj) { //The system comes with properties and methods that do not print for in.
return false;
}
return true;
},

Parsehtml:function (data, context, keepscripts) { //Parse string array node, for example: ' <li></li> ', [li element]

    Keepscripts is true, can parse Script,false cannot parse script tag
if (!data | | typeof data!== "string") {
return null;
}
if (typeof context = = = = "Boolean") { //If there is not a second parameter
keepscripts = context;
context = false;
}
Context = Context | | Document

var parsed = rsingletag.exec (data), //If it is a single label "<li></li>"
Scripts =!keepscripts && [];

if (parsed) { //creates this element directly and then puts it in the array to return
return [Context.createelement (Parsed[1])];
}

parsed = Jquery.buildfragment ([data], context, scripts);

     If Keepscripts is false, an empty array scripts will be passed in, Buildfragment will determine if there is a script in data and, if so, put an empty array into [script] and return []. If Keepscripts is true, the incoming false,buildfragment does not change it, or false.

if (scripts) {
JQuery (Scripts). Remove (); //Remove script tag
}

return Jquery.merge ([], parsed.childnodes);
},//eval can parse the JSON string, any format can be, the danger increases. Json.parse () can only parse the standard JSON string, which performs better than Eval.  

The Parsexml:function (data) { //Parse XML string is an XML document.
var XML, TMP;
if (!data | | typeof data!== "string") {
return null;
}

try {
TMP = new Domparser (); //ie6-8 use ActiveXObject to parse
XML = tmp.parsefromstring (data, "text/xml");

      IE9, an error occurs if the XML string is not a normal XML tag. Other browsers will not error, generating PARSERERROR nodes in the returned XML document
} catch (e) {
xml = undefined;
}

if (!xml | | xml.getelementsbytagname ("parsererror"). Length) {
Jquery.error ("Invalid XML:" + data);
}
return XML;
}, 

Globaleval:function (code) { //Changes the local variable to a global variable, for example: "Var a=1".
var script,
indirect = eval; //eval is both a keyword and a property under window. Directly write eval (code), the browser will be used as the JS keyword, error. So assigning a value, you'll see eval as the window's property.

Code = Jquery.trim (code); //code must be in the form of a string

if (code) {
if (Code.indexof ("use strict") = = = 1) { //Strict mode, eval cannot be used.
Script = document.createelement ("script");
Script.text = code;
Document.head.appendChild (script). Parentnode.removechild (script);
} else {
Indirect (code);
}
}
},  

Camelcase:function (String) { //Goto hump, ie under:-ms-transform-Mstransform, while other-moz-transform-> moztransform, So first replace the-ms-with ms-, so that it will only turn into Mstransform,m not capitalized. and other browsers need to capitalize
Return String.Replace (Rmsprefix, "ms-"). Replace (Rdashalpha, fcamelcase);
},

......

})

Come on!

jquery Source Code Analysis: The jquery tool method detailed 2

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.