Subject to a
- Used in JS
typeof
What types can you get - When to use
= =
When to use= = =
- What are the built-in functions in JS
RegExp, Array, Object, Boolean, String, Number, Function, Date, Math
- JS variables are classified into different types according to how they are stored, and describe their characteristics
- How to understand
JSON
JSON
Is just aJS
Object is also a data formatJSON.stringify()
JSON.parse()
knowledge
- Variable types
- Value types
Var a = 100 var b = a a = 200 console.log(b) // 100Copy the code
- Reference types
[Array object function]
Var a = {age: 20} var b = a.age = 21 console.log(a.age) // 21 reference types simply place values at an address and point to that address via a pointerCopy the code
The typeof operator
Typeof can only distinguish value types in detail, and object & for reference typesfunction
typeof undefined // undefined
typeof 'abc' // string
typeof 123 // number
typeof true // boolean
typeof {} // object
typeof [] // object
typeof null // object
typeof console.log // function
Copy the code
- Variable calculation
- Cast casting
- String splicing
- The = = operator
- If statement
- Logical operations
var a = 100 + 10 // 110
var a = '100'+ 10 / /'10010'100 = ='100' // trueConvert to the string 0 ==' ' // truetofalse
null == undefined // trueConverted to Booleanfalse
var a = true
if(a) {//... } var b = 100if(b) {//... } var c =' '
if(c) {//... } console.log(10 && 0) // 0 console.log(' ' || 'abc') / /'abc'console.log(! window.abc) //true// Determine if a variable is treated astrueorfalsevar a = 100 console.log(!! a)Copy the code
Topic 2
- How to determine exactly what a variable is
An array type
Variable instanceof Array
var arr = [] arr instanceof Array // true Copy the code
- Write a
Prototype chain inheritance
An example offunction Person(name, sex){ // todo ... this.name = name; this.sex = sex; } Person.prototype.getName = function(){ // todo ... console.log('name: ' + this.name); }; function Male(name, sex, age){ //todo ... Person.call(this, name, sex); this.age = age; } //todo ... Male.prototype = Object.create(Person.prototype); Male.prototype.constructor = Male; Male.prototype.getAge = function(){ //todo ... console.log( 'age: ' + this.age); }; var ruoyu = new Male('yym'.'male', 24); ruoyu.printName(); Copy the code
- describe
new
The process of an object
knowledge
- The constructor
functionFoo (name, age) {/ / constructor capitalize the first letter / / 1 / / 2. This name = name | |'1'
this.age = age || 23
this.class = 'class1'
// 3
}
var f = new Foo('yym', 24) var f1 = new Foo // If no argument is passed, what does new do without parentheses? 1. This = {} 2. This.__proto__ = foo.prototype 3.return this
Copy the code
- Constructor – extension
Var a = new Array() var a = new Array() var a = new Array(function Foo() {... Var Foo = new Function(...) Use instanceof to determine if a function is a constructor of a variableCopy the code
- Prototype rules
- All reference types (array object functions) have object properties that extend properties freely (
null
Except)
var obj = {}; obj.a = 100
var arr = []; arr.a = 100
function fn () {}
fn.a = 100
Copy the code
- For all reference types, there is one
__proto__ (implicit prototype)
Property, property is a normal object
obj.__proto__
arr.__proto__
fn.__proto__
Copy the code
- All of the functions, there’s one
Prototype (explicit)
Property, the property value is also an ordinary object
console.log(fn.prototype)
Copy the code
- All reference types,
__proto__
Property that points to its constructorprototype
Attribute values
obj.__proto__ = Object.prototype
Copy the code
- When trying to get a property of an object, if the object itself does not have the property, it will be destroyed
__proto__
(that is, of its constructorprototype
) to look for
function Foo(name, age) {
this.name = name
}
Foo.prototype.alertName = function() {console.log(this.name)} var f = new Foo()'yym')
f.printName = function() {
console.log(this.name)
}
f.printName() // yym
f.alertName() // f.__proto__
console.log(f.__proto__ === Foo.prototype) // true
Copy the code
- Properties of the loop object itself
var item
for (item inF) {// The advanced browser is already infor inAdd this judgment to ensure the robustness of the programif (f.hasOwnProperty(item)) {
console.log(item)
}
}
Copy the code
- Prototype chain
// Encapsulate DOM query, prototype chain example:function Elem(id) {
this.elem = document.getElementById(id)
}
Elem.prototype.html = function (val) {
var elem = this.elem
if (val) {
elem.innerHTML = val
returnThis // chain operation, return object}else {
return elem.innerHTML
}
}
Elem.prototype.on = function (type, fn) {
var elem = this.elem
elem.addEventListener(typeVar div1 = new Elem();'div1')
div1.html('< a > hello < / a >').on('click'.function() {
this.style.color = 'red'
})
Copy the code
function Foo(name, age) {
this.name = name
}
Foo.prototype.alertName = function() {
console.log(this.name)
}
var f = new Foo('yym')
f.printName = function() {
console.log(this.name)
}
f.printName() // yym
f.alertName() // f.__proto__
f.toString() // f.__proto__.__proto__
Copy the code
indtanceof
- Used to determine
Reference types
The method that belongs to which constructor
f instanceof Foo // trueFoo. Prototype f instanceof Object //true
Copy the code
Back to continue
From the interview questions to investigate knowledge points (2)
From the interview questions to investigate knowledge points (3)