Basic Review of Javascript (1) Type

Source: Internet
Author: User

This is the last article in the expression series from simple to deep, but recently the team has been busy and never been busy! However, if you like expressions, please rest assured that some basic principles of Javascript are common in your work, so I decided to spend some time organizing the basic knowledge and sharing it with you. I was planning to write an article at the beginning, but later I wrote it and found more and more articles. So I decided to write a series. All content in this series involves Javascript basics, and there are no trendy things, but I believe these basic things will help you understand those interesting things. Basic Review of Javascript (1) basic review of Javascript (2) Basic Review of Javascript in scope (3) Object-oriented (3) Yes, what I can think of when it comes to Javascript is interesting and fun! So what are the places for fun and why? Let's play together. Let's have a thorough understanding of Javascript. The content of this article includes: basic type Object and object basic packaging type value type and reference type function type basic type Javascript has five basic data types (also called simple data types ): undefined, Null, Boolean, Number, String, and one complex data type Object. Copy the code var undefinedVariable; var nullValue = null; var cnblogs = new Object (); var func = function () {}; typeof ("string"); typeof (100 ); typeof (true); typeof (undefinedVariable); typeof (cnblogs); typeof (undeclaredValue); typeof (nullValue); typeof (null) typeof (func) Tell me what the result is? Fun one: declared but unassigned and undeclared variables are both undefined fun two: only declared and assigned null, its value will be null fun three: typeof (Object) it turned out to be a function. 4. typeof (Null) is actually an object of Null and Undefined. Both types have only one value, that is, null and undefined. Logically, the null value indicates a pointer to a null Object, which is why typeof (null) returns the Object. And undefined is derived from the null value, so... fun 5: null = undefined is true. But think about it. Null and Undefined are two different types after all. Even if they are the relationship between the parent class and the subclass, the parent class and the subclass cannot be equal in C #, isn't it? In fact, null = undefined is a rigid rule. ECMA requires that they return true when performing equality tests, so they return true. It's like we have rewritten the equlas method in C. For how typeof (Object) returns the function, see the following Object and object. In the book Object and object Javascript advanced programs, "functions are objects in ECMAScript, not a data type ". It seems that the translator added the function. Since typeof (Object) returns the function, why does it mean that the function is not a data type? What is the relationship between objects and functions? In my opinion, an Object is actually a function, or we can say that an Object is a function name that is easy to understand. The official name is a constructor. Copy the code var p = new Object (); p. name = "jesse"; p. age = 18; function Person (name, age) {this. name = name; this. age = age;} var p2 = new Person ("jesse", 18); copy the Code. In the code above, if we treat the Object as a function name, then new Object () it is of the same nature as new Person. Use the new operator to get a function instance. The function here is already a class concept. So the Object here is actually a function. In this way, we can explain why typeof (Object) is a function. What is the complex Object we mentioned above? The Object is a function, but the new Object () is an object. At this point, we can figure out that the Object whose first letter is capitalized is a function, and the object whose first letter is lowercase is a data type. Therefore, we recommend that you use lower case for all types later. The basic types are string, number, and boolean. Uppercase strings, numbers, and Boolean are just a function. The result of calling these functions is, yes, it is an object. Finally, we cannot find the Undefined and Null functions. Therefore, the two data types are undefined and null (Why does typeof (null) Get the object) fun 6: Object is not the basic packaging type of the object type. We mentioned above that string, number, and boolean are the basic types. The biggest difference between basic types and complex types is that the basic types do not have the prototype attribute. This means that you cannot add methods or attributes to basic types at will. Var str = "str"; // typeof (str): stringvar strObj = new String ("str"); // typeof (strObj): object strObj. name = "strObj"; strObj. alert = function () {alert (this. name) ;}; strObj. alert (); // strObj str. name = "str"; // wrong... str. alert = function () {alert (this);} str. alert (); // this is wrong .... nothing is gonna happen. when copying the code, we also say that the String with the upper letter is a function, so new String ("str") gets an object instead of a string, We need to figure it out here. The question is, why are there some initial methods for the basic type of string? Isn't it a basic type? How does one add it? Str = str. concat ("str2"); strObj = strObj. concat ("str2"); strObj. alert (); // then the returned string is no longer an object, so there is no alert method here. Str is a string type variable. Remember that it is not an object. It should not have a method, so where does its contact method come from? Str is called in the background. when you contact, you actually secretly completed several steps: Create a String-type instance based on str and call the specified method on the instance to destroy the instance. The three steps are as follows: var str2 = new String (str); str = str2.concat ("str2"); str2 = null; we can call String, Number, and Boolean the encapsulation type, they are like the custom types in C. But do not forget that our real basic types are string, number, and boolean. The object constructed with String belongs to the object type. Fun 7: String is not a string value type or a reference type. We have mentioned five basic types: string, number, boolean, null, and undefined. They are all value types. There is only one reference type in Javascript, that is, our complex type object. So some people may be curious. What types are there, such as Date, Regex, and Arrary? In fact, the concept here is a bit confusing. If you understand the difference between the above Object and the object, it may be better understood. We can regard function as the class keyword in C #. We can use class to define classes, and we can also use function to define classes in Javascript. Define the class in C #: namespace ConsoleApplication1 {class Person {public string Name {get; set;} public int Age {get; set ;}} class Program {static void Main (string [] args) {var p = new Person (); Console. writeLine (p. getType (); // leleapplication1.person Console. readLine () ;}} defines the class in Javascript: copy the code function Person (name, age) {this. name = name; this. age = age;} var p = new Person (); typeof (p); // copy the code of the object Do you find the difference? If we use functions in Javascript to define classes, their instances will always be objects, including those Date, Array, and RegExp native. Typeof (new Date (); // objecttypeof (new Array (); // objecttypeof (new RegExp (); // object fun 8: if all objects are objects, how can I know whether the object is a Date or Person instance? You can use instanceof. End-to-End fun: I used function to create a Person class and then used new to get an instance of Person. As a result, it is not of the Person type. This is like giving birth to a child for him to eat and wear, but he does not have your last name. How selfless and great can this come out! Function types include function declaration and function expression. Function Declaration has a priority. It can be used before declaration, but not an expression. Copy the code sayGoodNight (); // rightsayHello (); // wrong var sayHello = function (name) {alert ("Hello," + name) ;}; function sayGoodNight (Name) {alert ("Good Night," + name);} In addition, function expressions can add attributes as they are in an object. Var sayHello = function (name) {alert ("Hello," + name) ;}; sayHello. age = 18; sayHello. sayAge = function () {alert ("I am" + 18);} sayHello. sayAge (); // I am 18. But what is a function expression like? It cannot be instantiated, but attributes can be added at will. What is the difference between attributes and objects? As we have mentioned above, an object is actually an object instance. We also have an upper-case Function. Will the relationship between it and function be the same as that between String and string? (The following content is hard work and careful !) Var sayHello = new Function ('name', 'alert ("My name is" + name); '); sayHello ('jesse'); sayHello instanceof Function; // true var sayHello2 = function (name) {alert ('My name is '+ name) ;}; sayHello2 instanceof Function; // true we call the Function above to construct a Function. Neither function declaration nor function expression is used. In any case, this is the third method for creating a function. Although it is certainly not used by many people, it is a string for both parameters and function bodies, isn't it a crash? What's tricky? The so-called function expression actually uses a variable to receive a function object. The function object is a Function instance. The functions written using Function Declaration are also Function instances. Function sayHello3 (name) {alert ('My name is '+ name);} sayHello3 instanceof Function; // true, but so on. The String, Date, array is all function type, so is Function? We mentioned earlier that all function instances are of the object type. However, for the Singular Function of function, its instance is still of the function type, and we can use the Function instance to create instances. The class we used to create with function is not a class, but a Function instance. Function Person (name) {this. name = name;} Person instanceof Function; // true. Let's take a look at the self-executed function. It may be better: (Function () {alert ("something... ") ;}(); in fact, the function () {} above returns a function instance to us, so we can execute it directly. In this case, function should be the most special type in Javascript. Fun 10: All functions are function instances. Fun 11: the Function itself is also a Function

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.