Subject to a

  • Used in JStypeofWhat 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 understandJSON
    • JSONIs just aJSObject is also a data formatJSON.stringify() JSON.parse()

knowledge

  1. 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
  1. 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 isAn array type
    • Variable instanceof Array
    var arr = []
    arr instanceof Array // true
    Copy the code
  • Write aPrototype chain inheritanceAn example of
    function 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
  • describenewThe process of an object

knowledge

  1. 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
  1. 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
  1. Prototype rules
  • All reference types (array object functions) have object properties that extend properties freely (nullExcept)
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 onePrototype (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 constructorprototypeAttribute 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
  1. 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
  1. 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

  1. indtanceof
  • Used to determineReference typesThe 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)