Understanding javascript prototype and scope series (1) -- everything is an object

Source: Internet
Author: User

Understanding javascript prototype and scope series (1) -- everything is an object
The focus of "Everything is an object" is to understand the concept of "object. -- Of course, not all objects are objects, and the value type is not objects. First, let's take a look at a common function in javascript -- typeof (). Typeof should be our old friend. Who has never used it? There are several types of typeof function output, which are listed here: copy the code function show (x) {console. log (typeof (x); // undefined console. log (typeof (10); // number console. log (typeof ('abc'); // string console. log (typeof (true); // boolean console. log (typeof (function () {})); // function console. log (typeof ([1, 'A', true]); // object console. log (typeof ({a: 10, B: 20}); // object console. log (typeof (null); // object console. log (typeof (ne W Number (10); // object} show (); copy the code and the above Code to list the type identifiers in the typeof output. The preceding four types (undefined, number, string, boolean) is a simple value type, not an object. In the remaining cases, functions, arrays, objects, null, and new Number (10) are all objects. They are all reference types. It is very easy to judge whether a variable is an object. Typeof is used for determining the type of the value type, and instanceof is used for determining the type of the reference type. Var fn = function () {}; console. log (fn instanceof Object); // true is good. The Object mentioned above is a half-day Object. You may often work on objects and have to deal with living objects in your life. Some single persons who are mentally abnormal or joking are still worried about the "object not found" system prompts. How can we define objects in javascript? Object-a set of several attributes. Objects in java or C # come out of a new class, and contain fields, attributes, and methods. The rules are very strict. But javascript is more casual-array is an object, function is an object, object or object. Everything in the object is a property, only the property, no method. How can this method be expressed? -- Method is also an attribute. Because its attribute is represented as a key-value pair. In addition, for more fun, javascript objects can expand attributes at will without class constraints. As we all know, we will not emphasize it any more. In the above Code, obj is a custom object, where a, B, and c are its attributes, and the attribute value in c is an object, it has two attributes: name and year. This may be easy to understand. Can functions and arrays define attributes like this? -- Of course not, but it can be in another form. In short, the stream of functions/arrays, as long as it is an object, is a set of attributes. Take the function as an example: copy the code var fn = function () {alert (100) ;}; fn. a = 10; fn. B = function () {alert (123) ;}; fn. c = {name: "Wang fupeng", year: 1988}; In the code above, the function is assigned three attributes as an object: a, B, and c. Obviously, is this a set of attributes. Q: Is this useful? A: Check jQuery source code! In jQuery source code, the variable "jQuery" or "$" is actually a function. If you don't believe it, you can ask our old friend typeof to verify it. Console. log (typeof ($); // functionconsole. log ($. trim ("ABC"); verify your identity! It is indeed a function. The commonly used $. trim () is also a function, which is often used. You don't need to verify it! Obviously, this adds a trim attribute to the $ or jQuery function. The attribute value is a function that intercepts spaces before and after the function. Compared with java/C #, the first thing to explain in javascript is that if the type is the most basic and commonly used, it is not intended to be described in this section. The second thing to explain is the content of this article-everything (reference type) is an object, and the object is a set of attributes. What you need to know most is the object concept, which is totally different from java/C. So remember!

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.