Basic Review of Javascript (1) Type

Source: Internet
Author: User
All the content in this series is related to the Javascript basics, and there are no fashionable things, however, I believe that these basic things will help you understand the interesting things. The last part of the expression series is to continue from simple to deep, but recently the team suddenly got busy, never 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.

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?

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

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

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(); // 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? 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 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

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!

For more articles about Javascript BASICS (I), refer to the PHP Chinese website!

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.