The first JS article, today I will start from the basic section, as well as three mountains began to talk about their understanding of JS, there is no place, please tell the great God to modify
Let's start by understanding some of the basics of JS,
The first is the data type, JS has two data types, the original type and the reference type , we use the typeof operator to determine what type of data,
It is worth mentioning thattypeof can only differentiate between value types , and reference types other than function will all return object including null,
So the value that typeof can detect is only
Undefined, string, number, Boolean, function, Object
typeofUndefined//undefinedtypeof"Zhangsan"//stringtypeof100// Numbertypeof true //Booleantypeof functionA () {}//functiontypeof NULL //Objecttypeof{}//Objecttypeof[]//Object
You can see that null,[],{} has returned an object, which is not what we want to see, how to differentiate a reference type, we can use the instanceof operator to differentiate,
[] instanceof array //trueinstanceof array //False instanceof Object //truenullinstanceof Object //false
The instanceof operator uses the left side as the reference type to be determined, the appropriate reference type on the right, and true if the same, false
You can see that instanceof solves the problem of reference type differentiation, but instanceof is not a very strict verification method, and I will mention in the following sections
We all know that JS is a weakly typed language, so JS will automatically type conversion, = = Only check the value is equal, = = = Check the value not only to check whether the type is equal
Variable calculation----cast in JS
// 0, "", false, [] with = = Test is the same,0 = = "" //0->false and ""->false to compare returns True[] = = "" // true ({}) = = "" //false because {} is used directly to compare the reported type error, so the () operator is required to wrap
——————————————————————————————————————————————————————————————————————————————————
null = = undefined //truenull = = = undefined //False
From the above code can be a simple understanding of JS type conversion, the third line of code is particularly interesting, in JS if only to determine the = =,
Null and undefined will cause the same type conversion, using this feature, we can write code in the future with
similar to Obj.name = = NULL to determine whether obj.name is null and whether it is undefined.
The built-in objects in JS include: 11
String number boolean--Wrapper type
Array REGEXP Math Date
Error
Function Object
Global: is a globally scoped object: replaced by window in the browser
So what is a wrapper type: A value that holds the original type and provides an API to manipulate the original type value?
In JS, the original type of the value itself does not have any function, such as: Var a=12.652; A.tofixed (2);
A is how to call the Tofixed () function, because the wrapper type object is automatically created in JS whenever you try to call a function on a value of the original type
The process of building a package type is divided into the following steps:
1. When attempting to call any function on a value of the original type, first determine the type name of the original type
2. Automatically create the corresponding wrapper type object, save the original type value that needs to be manipulated
3. Invoke the API provided by the wrapper type Object
Wrapper Object Auto-release after 4.API call
Let's show a small example:
var str= "haha"; Str.len=5; Console.log (Str.len)
Deft on hand's little buddy should have tried it out, yes undefined, so where's our . Len ?
The principle is such that str.len=5, new String (str). len= 5;
After execution, the new String () has no variable to receive, released !
Console.log (Str.len), Console.log (new String (str). len);//The new string at this time is the newly arrived wrapper type and does not recognize the Len attribute
Improvement: We should take this approach
var str=New String ("haha"); Str.len=5; // at this point, the new String () is referenced by the STR variable and will not be released! Console.log (Str.money); // 5
Well, today's share is here, if there is a supplement or error, please remind me to correct, please give more advice
On JS Foundation and built-in objects