This is the 7th day of my participation in the August More Text Challenge.
preface
“JavaScript Object Oriented Essentials” combing
JavaScript type
There are two types of JavaScript types, primitive and reference.
Primitive types are stored as simple data values, and reference types are stored as objects, which are essentially references to memory locations.
Reference types, specifically. JavaScript uses a variable object to track the lifetime of a variable. The original value is stored directly in the variable object, and the reference value is stored in the variable object as a pointer to where the actual object is stored in memory.
The original type
JavaScript initial have five primitive types: Boolean, number, string, null, and undefined.
Each primitive data type has a literal form.
// String name='zhiqiang'; var selection='a'; //number var count=235; Var cost = 1.51; //boolean var found=true; //null var object=null; //undefined var flag=undefined; var ref; console.log(ref); //undefinedCopy the code
Each primitive type of value has its own storage space. Changes in one variable do not affect the values of other variables. (If the value of a variable is a primitive type)
var color1='red';
var color2=color1;
console.log(color1) //red
console.log(color2) //red
color2 = 'blue'
console.log(color1) //red
console.log(color2) //blue
Copy the code
Identify primitive types using the Typeof operator
Console. log(typeof '123') // String console.log(typeof 10) //number console.log(typeof 5.1) // Number console.log(typeof True) // Boolean console.log(typeof undefined) //undefined console.log(typeof null) //"object" // This has been identified by TC39 as an error // Nulljinxingbijiao console.log(value === null) It is recommended to use == rather than == because == will result in type conversionCopy the code
The original method
Primitive types such as strings, numbers, booleans, and so on have methods.
A method that is not an object has a primitive wrapper type.
The original wrapper type is a reference type.
Reference types
Create object, dereference object, Add and delete object attributes (delete obj.name)
Instantiation of built-in types
Array, Date, Error, Function, Object, RegExp
Create method: new, literal
Access attribute point operators and brackets
Identify reference types (instanceof operator), identify arrays (array.isarray ())
Original package type
Take a closer look at the original package type
Var name = "Nicholas" var firstChar = name.charat (0) console.log(firstChar)Copy the code
Why does name have a method here because this is how JavaScript is executed
var name = "Nicholas"
var temp = new String(name)
var firstChar = temp.charAt(0)
temp = null
console.log(firstChar)
Copy the code
Let’s do a more complicated example
var name = "Nicholas"
name.last = "Zakas"
console.log(name.last)
Copy the code
JS engine execution process
var name = "Nicholas"
var temp = new String(name)
temp.last = "Zakas"
temp = null
var temp = new String(name)
console.log(temp.last)
temp = null
Copy the code
Each time a property is called, a temporary primitive wrapper type is created and released upon execution