Basic Review of Javascript (1) type, review of javascript

Source: Internet
Author: User

Basic Review of Javascript (1) type, review of javascript

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) Type
  • Basic Review of Javascript (ii) Scope
  • Basic Review of Javascript (3) Object-oriented

Yes. What I can think of when it comes to Javascript is fun and fun! So what are the places for fun and why? Let's play together. Let's have a thorough understanding of Javascript. Content of this article:

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.

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: declared but unassigned values and undeclared variables are all undefined.
Fun 2: its value is null only when it is declared and assigned null.
Fun 3: typeof (Object) is actually a function
Fun 4: typeof (null) is actually an object

Both Null and Undefined 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.

Object and object
In the book of 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.

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);

In the above Code, if we regard the Object as a function name, the new Object () and new Person () are of the same nature. 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 of the object Type

Basic packaging type
The string, number, and boolean types are 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):objectstrObj.name = "strObj";strObj.alert = function () { alert(this.name);};strObj.alert(); // strObjstr.name = "str"; //wrong...str.alert = function () { alert(this);}str.alert(); // this is wrong.... nothing is gonna happen.

At the same time, we also mentioned that the String with the upper letter is a function, so the 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? Here, the background secretly completed several steps when calling str. contact:

  • Create a String-type instance based on str
  • Call the specified method on the instance
  • Destroy this instance

Think of the three steps as follows:

var str2 = new String(str);str = str2.concat("str2");str2= null;

We can call String, Number, and Boolean the encapsulation type, just like the custom type 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 and reference type
The five basic types are described above: string, number, boolean, null, and undefined, all of which are 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 classes 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()); // ConsoleApplication1.Person  Console.ReadLine(); }}}

In the Javascript definition class:

function Person(name,age){ this.name = name; this.age = age;}var p = new Person();typeof(p); //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: all objects
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 Type
Function types include function declaration and function expression. Function Declaration has a priority. It can be used before declaration, but not an expression.

sayGoodNight(); // rightsayHello(); // wrongvar 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? 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; // truevar sayHello2 = function (name) { alert('My name is' + name);};sayHello2 instanceof Function; // true

We called 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

However, wait. The String, Date, and Array types we mentioned earlier are all function types. Is that true for 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 above function () {} will return 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: Function itself is also a function

Finally, let's summarize:

  • Javascript has five basic types: string, number, boolean, null, and undefined.
  • Another complex type of object is actually a function instance.
  • Except for the Function constructor in the system, all function instances are of the object type.
  • Date, Array, and RegExp are both function types and Function instances. Likewise, their instances are of the object type.

It seems that there are not many summary, right? In fact, javascript is a very powerful function, and the scope and object-oriented knowledge are also closely related to it. Let's look at the scope issue in the next article. Thank you for your attention!

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.