JavaScript prototypes and scopes (1) Everything is an object.

Source: Internet
Author: User
Tags arrays log trim

The emphasis of the phrase "everything is an object" is how to understand the concept of "object".

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

First, let's look at a commonly used function--typeof () in JavaScript. typeof should be an old friend of ours, and who else hasn't?

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

function Show (x) {
    
            Console.log (typeof (X));    Undefined
            Console.log (typeof);   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 central type identification of the TypeOf output, where four (undefined, number, String, Boolean) are simple value types, not objects. The remaining few cases-functions, arrays, objects, NULL, new number (10) are objects. They are all reference types.

It's very simple to judge whether a variable is an object. The type of the value type is judged using the TypeOf, the type of the reference type is judged by the instanceof.

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

Well, the above said a half-day, you may also often in the work to deal with the object, in life still have to deal with living objects. Some people who are psychologically abnormal or playful are also concerned about the "object not found" system prompts. So what exactly is the definition of an object in JavaScript?

Object--A collection of several properties.

The objects in Java or C # are new one class, and there are fields, properties, methods, very strict rules. But JavaScript is more casual--an array is an object, a function is an object, an object, or an object. Everything inside the object is a property, only a property, no method. So how does this approach represent? --The method is also a property. Because its properties are represented as the form of key-value pairs.

And, more fun, objects in JavaScript can be arbitrarily extended attributes, without class constraints. This should all be known, it will not be emphasized.

First, the most common example:

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

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

This may be better understood, so can functions and arrays also define attributes? -Of course not, but it can be in another form, in short, the flow of functions/arrays, as long as the object, it is the collection of attributes.

Take the function as an example:

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

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

You ask: Is this useful?

Answer: You can look at the jquery source!

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

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

The Ming! It's really a function. So we commonly used $.trim () is also a function, often used, do not have to test it!

Obviously, this is the addition of a trim attribute on the $ or jquery function, which is a function that intercepts the spaces before and after the action.

JavaScript and java/c#, the first thing to explain the most is the type, because if the 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 an object is a collection of attributes. The most need to understand is the concept of the object, and java/c# completely different. So remember to remember!

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

Specific reasons, and listen to let's!

Author: cnblogs Wang Fu

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