JS (prototypes and prototype chains)

JS (prototypes and prototype chains)

Topic 1. How to accurately determine whether a variable is an array type

Topic 2. Write an example of prototype chain inheritance

Topic 3. Describe the process of new an object

Topic 4. How to use prototype chain in zepto source code

1. Constructor

Capital letters are generally constructors

new process:

1.var f = new Foo('zhangsan',20) pass the parameters in, this in the function will become an empty object

2.this.name = name;this.age = age;this.class = 'class-1' is assignment; return this is the actual operating mechanism

3. Assign to f after return, f has f.name = zhangsan, f.age = 20, f.class = 'class-1'

4. Continue to f1, then f1.name = lisi, f1.age = 22, f1.class = 'class-1'

2. Constructor (Extension)

1. var a = {} is actually syntactic sugar for var a = new Object()  (the constructor of a is an Object function)

2. var a = [] is actually syntactic sugar for var a = new Array()    (the constructor of a is an Array function)

3. function Foo(){...} is actually var Foo = new Function(...)      (The constructor of Foo is a Function function)

4. Use instanceof to judge whether a function is a constructor of a variable (judging whether a variable is an "array" variable instanceof Array)

3. Prototype Rules

1. All reference types (arrays, objects, functions) have object characteristics, which can be freely extended attributes (except "null")

2. All reference types (arrays, objects, functions) have a _proto_ property (implicit prototype property), and the property value is an ordinary object

3. All functions have a prototype (explicit prototype) property, and the property value is also an ordinary object

4. For all reference types (arrays, objects, functions), the _proto_ property value (implicit prototype property) points to the "prototype" property value of its constructor

4. Prototype chain

五、instanceof

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325167758&siteId=291194637
Recommended