Deep understanding of JavaScript prototypes and Closures (1)--Everything is an object

Source: Internet
Author: User

Reproduced, original

The point of the phrase "Everything is Object" is how to understand the concept of "object".

--Of course, not all of them are objects, and value types are not objects.

First of all, let's look at a common function--typeof () in JavaScript. typeof should be an old friend of ours, and who's not going to use it?

There are several types of typeof function output, listed here:

       function Show (x) {            Console.log (typeof (X));    Undefined            Console.log (typeof (Ten));   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 (new number));  Object        }        show ();

The above code lists the set type identifier for the typeof output, where the top four types (undefined, number, String, Boolean) are simple value types, not objects. The remaining cases--functions, arrays, objects, NULL, new number (10) are all objects. They are all reference types.

It is very simple to judge whether a variable is not an object. The type of the value type is judged with typeof, and the type of the reference type is judged with instanceof.

var fn = function () {};console.log (fn instanceof Object);  True

Well, it says a half-day object, you may also often in the work to deal with objects, in life also have to deal with living objects. There are some people who are mentally abnormal or who love to joke, but also for the system prompt "can't find the object" brooding. So what is the definition of an object in JavaScript?

Object-A collection of several properties.

The object in Java or C # is a new class, and there are fields, properties, methods, strict rules. But JavaScript is more casual-arrays are objects, functions are objects, objects or objects. Everything inside the object is a property, only attributes, no method. So how does this approach show? --The method is also a property. Because its properties are represented in the form of a key-value pair.

And, more fun, the objects in JavaScript can be arbitrarily extended properties, without class constraints. This should all be known and no longer emphasized.

Let's say one of the most common examples:

In the above code, obj is a custom object, where a, B, and C are its properties, and the property value of C is an object, and it has the name, year two properties.

This may be better understood, so can functions and arrays define properties as well? --of course not, but it can be in a different form, in short the function/array flow, as long as the object, it is a collection of attributes.

Take the function as an example:

        var fn = function () {            alert (+);        };        Fn.a = ten;        fn.b = function () {            alert (123);        };        FN.C = {            name: Wang Fu,            year:1988        };

In the previous code, the function is assigned a, B, and C three properties as an object--obviously, this is the collection of properties.

You ask: Does this help?

Answer: Can look at the jquery source code!

In jquery source code, "jquery" or "$", this variable is actually a function, do not believe you can call our old friend typeof to verify.

Console.log (typeof ($));  Functionconsole.log ($.trim ("ABC"));

Yue Heyue It's really a function. Then we often use the $.trim () is a function, often used, you do not have to test it!

Obviously, this is a trim property added to the $ or jquery function, and the property value is a function that intercepts the space before and after.

JavaScript and java/c#, the first most need to explain is the weak type, because the weak type is the most basic usage, and most commonly used, do not intend to do a section.

The second thing to explain is the content of this article-everything (reference type) is an object, and the object is a collection of properties . The most need to understand is the concept of objects, and java/c# completely different. So, remember to remember!

Finally, there is a question. In the output type of typeof, function and object are objects, why do you want to output two answers? It's called object. --Of course not.

Specific reasons, and listen to tell!

Deep understanding of JavaScript prototypes and Closures (1)--Everything is an object

Related Article

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: 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.