JavaScript series-everything is an object

Source: Internet
Author: User

JavaScript series-everything is an object
1. To determine the object type 1.1.typeof operator, you must first realize that typepof is an operator and its operation requires a parameter. The return value is the parameter type. Typeof usage method typeof parameter // use method 1 typeof (parameter) // use method 2... these two methods are equivalent. The return value of typeof: undefined, boolean, number, string, object. javaScript mainly has five basic data types and one type of referenced data. Therefore, for the return value of typeof, it is easy to confuse the following: typeof null; // "object" null indicates an empty object in js. Therefore, typeof null = "object" is also justified. 1.2.instanceof operator instanceof is also an operator. A parameter is also required during the operation to determine whether an object is an instance of the given constructor. the return value is true or false; instanceof method parameter1 instanceof parameter2 // The first parameter of method is an object, and the second parameter is the return value of instanceof returned value of instanceof of two methods equivalent to instanceof function parameter2, there are only two types of true or false; 1. for the basic data type, false is always returned; console. log (null instanceof Object); // falseconsole. log (true instanceof Boolean); // falseconsole. log (10 instanceof Number); // Falseconsole. log ("sad" instanceof String); // false 2. for an object, determine whether it is an instance of a constructor; var object ={}; console. log (object instanceof Object); // true function A () {} console. log (A instanceof Function); // true var B = function () {}; var B = new B (); console. log (B instanceof B); // true 1.3.instanceof principle typeof is relatively simple, mainly based on a data type. Here we will mainly explain how instanceof works. take the following example: var Animal = function () {}; var Dog = function () {}; // The subClass inherits the function extend (subClass, superClass) of the parent class) {var prototype = Object. create (superClass. prototype); subClass. prototype = prototype; subClass. prototype. constructor = subClass;} extend (Dog, Animal); var dog = new Dog (); console. log (dog instanceof Dog); // trueconsole. log (dog instanceof Animal); // This example above true declares two Class, animal and dog. instantiate an object dog with the dog class. According to our understanding, dog belongs to the class of dog. isn't a dog an animal at that time? Therefore, it is reasonable to output true for both of the above two output results. After the above code is executed, the memory conditions are roughly the same! After learning about the memory, let's analyze the code execution process. When you run the dog instanceoof Dog command to determine whether the dog object is an instance of the Dog class, 1. the interpreter checks whether the dog is an object. If the dog is not an object, false is returned. if it is an object, skip to the second part. 2. The Interpreter first obtains the built-in properties of the dog and then obtains the prototype of the Dog function ). 3. Compare whether the built-in attribute of dog is the same object as that of Dog. prototype. If so, true is returned. When Step 1 is executed, because true is returned. The execution of the sentence ends. When executing dog instanceoof Animal, perform the following steps to the third part to determine the built-in attributes of dog and Animal. prototype is not an object. Next, execute step 4. if the built-in attributes of the dog object are not the same as those of the Animal class, the built-in attributes of the object pointed to by the dog built-in attributes are retrieved. determine whether the built-in attributes of the object to which the dog property points are Animal. indicates whether prototype points to the same object. If prototype points to the same object, true is returned. otherwise, search up along the built-in property chain of the object and trace back to null (Objece. prototype. _ proto __= = null. In short, when instanceof is compared, the comparison rule is to compare all objects on the built-in attribute chain on the object with the prototype of the given constructor, until equal values are found. If the two are not found to be equal after comparison, false is returned. 2. All objects are 2. 1. Why is everything an object? Everything is an object. Where should I start !! See the following code? Console. log (Function instanceof Object); // truefunction a () {}; console. log (a instanceof Object); // true if a function is an Object, and the Function constructor is also an Object, so we have no reason to oppose that everything is an object. However, how can we prove that functions and function constructors are all objects and how can we design them? We know that if it is an object. As an Object, you must have the most basic attribute of the Object-the built-in attribute (the built-in attribute is an attribute of any Object). In addition, the built-in attribute originally originated from the Object. prototype. if so, we can draw a conclusion that both Function and fucntion have a built-in attribute, and the built-in attribute comes directly from or indirectly from the Object. prototype. now we use the code to prove whether it looks like this: console. log (Function. _ proto __. _ proto __= = Object. prototype); // true if the output result is true, the structure in the memory should look like this: here, we can explain that Function is an object. Next we will explain that function is also an object. In the same way, since a function is an object, it certainly has built-in attributes, and the function is an instance of the Function. Therefore, its built-in attributes must come from the Function. prototype. function. what is prototype? Console. log (Function. _ proto __= = Function. prototype); // do you understand this? In the past, the built-in and prototype attributes of a Function were actually the same object. Now, let's take a comprehensive picture to analyze the relationship between function Function Object. prototype. Here we reveal that everything is an object design. But if we want to understand the entire design architecture, what else do we need to understand? 1. the constructor of the built-in attribute of the Function object points to the Function. because constructor points to Function ==> Function. _ proto _ is an instance of Function ==> Function. _ proto _ is a function. OK... verify: console. log (typeof Function. _ proto _); // do not use instanceof for function determination. If instanceof is used, false is returned. to find out the cause, see how instanceof works. This proves our inference. However, the following question comes: all objects have a built-in attribute. In other words, the Object function should have a built-in [_ proto _] attribute, and the Object. prototype should also have a built-in [_ proto _] attribute. OK .. if the above picture is complete, the result should be as follows: in the above picture, we can see that no matter who the constructor of an object is, its built-in attributes will eventually return to the top Object. prototype. that is to say, everything is an object. Note: The image above is not so easy to understand. So, let's talk about how to understand the image above? 2. Why is this design? From an aesthetic point of view, the design above is really not beautiful. First, it is not simple. Second, it's hard to see what the image is trying to do! Understanding the image above requires three basic guiding ideology: 1. Everything is an object. 2. An object always has a built-in attribute that points to the prototype attribute of its constructor. 3. The function always has a prototype attribute, which is assigned to the built-in attributes of the instance when the function is instantiated. I understand the three points above and the following code to see if you can answer correctly? Function a () {}; console. log (. _ proto _ = Function. prototype); console. log (Function. _ proto _ = Function. prototype); console. log (Function. _ proto __. _ proto _ = Function. prototype) console. log (Object. _ proto _ = Function. prototype); // The code is used to prove the second point. The first point we have proved function a () {}; console in 2.1. log (. _ proto _ = Function. prototype); var object ={}; console. log (object. _ proto __= = Object. prototype) // The code is used to prove Three points are precisely because of the existence of the above three types of knowledge guidance, so we will make JS so dazzling. Now, based on these three ideas, do you have a better design than the previous image !! Note: javascript is not strictly object-oriented. It can be said that everything is just a pile of skills. However, the inspiration from this language can indeed be deeply thought-is there a better design for Object-Oriented Systems? 3. The summary is to list the key points of this article. First, analyze the comparison between typeof and instanceof to understand the working principle of instanceof. Because in the second part, you need to use this function in large quantities. Secondly, we will analyze this point gradually, and everything is the object of this point of view. Then, use an instance to prove your point of view. Once again, after proving that everything is an object, we will analyze it and use the picture to draw out the outline of the entire design step by step. The focus is to grasp the last picture. Because all the purposes are to prove the last image. Finally, the core guiding ideology of this design is proposed. You can read the second part more carefully than following this guiding ideology.

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.