Js senior: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : ////////////////////////////////////////

Basic Thinking of Programming aspect Object (Model)

  1. Write instance properties inside the constructor
  2. Write shared data on the prototype

/////////////////////////////////////////////////////////////////

Equal conversion rules: (Note: it is downward matching, one by one matching, matching directly apply)

  1. NaN is not equal to any value, including himself
  2. Null is not equal to any value except null and undefined
  3. Undefined is not equal to any value except null and undefined
  4. If there are numbers or booleans on both sides of the operands, both are converted to numbers for comparison
  5. If there are strings on both sides of the operands, they are converted to strings for comparison
  6. If both sides of the operand are complex data types, compare addresses

//////////////////////////////////////////////////////////////////

Boolean interrupt: && : Searches left to right for false values (Boolean of type false). If found, the logic breaks and returns false values not true or false but the value itself that is Boolean of type false

=== congruence is the comparison of type and value equals equals and equality is implicitly converted to the same type for comparison

/////////////////////////////////// function Person(){

} var p = new Person();

console.log(p.proto === Person.prototype); // True instance’s __proto__ and constructor’s prototype point to the same thing.

/////////////////////////////////

console.log(p.constructor == Person); // true console.log(p.constructor == Person.prototype.constructor); // true

///////////////////////////////////////

Setting principles:

  1. If the object does not have the property, it is added with the assigned value.
  2. If the object has the property, the original value is overridden

Summary: Property search principle: Search along the prototype chain Property setting principle: if there is, overwrite, if there is not, add

///////////////////////////////////

HasOwnProperty () Syntax: object. HasOwnProperty (” property “); Function: Checks whether this attribute belongs to the object itself, if true, false if not

In operator: Checks whether the property can be accessed by the object. Returns true if it can be accessed, and false if it cannot

PropertyIsEnumerable (): Syntax: object propertyIsEnumerable(” propertyIsEnumerable “) 2. Check whether an attribute is traversable. This parameter is returned true only when the preceding two conditions are met

Object a. isprotoTypeof (object B); Is object A A prototype of object B

///////////////////////////////////////////////

What is a closure? A closure is a function that reads variables inside other functions. A closure can be understood simply as a function defined inside a function. Essentially, a closure is a bridge between the inside of a function and the outside of a function;

You can operate on private variables and if a function is referenced by a variable other than its parent, it forms a closure

Closures have three features: 1 function nested functions 2 Arguments and variables outside the function can be referenced inside the function 3 arguments and variables are not collected by the garbage collection mechanism

Advantages: 1 protect the security of variables within the function; 2 Maintain a variable in memory for caching; 3 Anonymous self-executing functions can reduce memory consumption

Disadvantages: 1 memory leak (fix: manually assign null to variables after use) 2 Performance loss due to cross-domain access involved in closures (fix: store cross-scope variables in local variables and access local variables directly to mitigate the impact on execution speed)

//////////////////////////////////////////////////////////////

ValueOf (): Converts an Object to its original value (simple data type), but the default (valueOf on Object.prototype) returns only the Object’s own special value: valuOf() on a date Object returns a timestamp (because the date prototype has its own valueOf method).

ToString (): Converts an object to a string. The toString method is provided on every built-in object prototype. ToLocaleString (): the same as toString, except for date objects

console.log(date.toString()); Mon Sep 03 2018 18:18:58 GMT+0800 (Chinese standard time) console.log(date.tolocalestring ()); // Mon Sep 03 2018 18:18:58 GMT+0800 (Chinese standard time) console.log(date.tolocalestring ()); // Local time

Summary: valueOf(): converts to a simple data type, and by default returns the object itself toString(): converts to a string

ValueOf &&toString = valueOf &&toString = valueOf &&toString = valueOf &&toString = valueOf &&toString = valueOf &&toString Call rules:

  1. The valueOf method is first called to try to convert the object to a simple datatype. If not, the toString method is investigated to convert the object to a string
  2. An error is reported if the object calls both valueOf and toString, and neither is converted to a simple data type

//////////////////////////////////////////////////////////////

Since the code inside our self-calling function does not affect the whole world, as in sandbox mode, we also say that self-calling functions are sandbox mode

Sandbox mode: is a separate and encapsulated environment where internal code does not affect external self-calling functions: a function is created and called at the same time

/////////////////////////////////////////////////////////////////

Inheritance: copy the properties and methods of an Object. Inheritance: An instance Object created by a constructor can inherit any member of the prototype directly. This method creates and returns an empty Object whose prototype is obj console.log(instance.proto === obj) //true

///////////////////////////////////////////////

Function fn(){function fn(){… Var fn = function(){… } 3 constructor create Function –var fn=new Function(parameter 1, parameter.. , function body)– Function is also an object,new out

///////////////////////////////////////////////////////

TXT. AddEventListener (“keyup”, function(){try(..)) catch(e){.. }} Try ()catch(){… }, will prevent error reporting

Eval (): Executes a string as js code

////////////////////////////////////

There are four modes of calling a function: 1 Function call pattern fn() this refers to window 2 object (method) call pattern obj.fn() or obj”fn” this refers to obj 3 constructor call pattern var p = new Person() this refers to newly created instance object 4 Call takes several arguments. The first argument is used to change the this reference in the function, and the rest are used as arguments to call the function when the call argument is not available, or when the first argument is null. So this inside the function refers to window

A pseudo-array is an object with a 1 digit subscript and a 2length length attribute that can be traversed like an array, but 3 cannot be traversed like an array

///////////////////////////////////////////////////

Common pseudo-arrays are:

  1. arguments
  2. document.getElementsByTagName()
  3. The jQuery object

The array push method adds items and automatically maintains the pseudo-array’s length attribute JOIN (): the array method used to concatenate each item in the array

Pseudo arrays is a pseudo Array objects turn come true: obj = Array. Prototype. Slice. The call (obj); Slice () var arr2 = arr.slice();

Fn. apply(pointing to, pseudo-array or array); fn.apply(pointing to, pseudo-array or array); Fn. call(pointing to, arg1, arg2,….) ; Apply’s features: Apply’s tiling: Takes each item in an array as an argument to a borrowing function

Bind (1) creates and returns a new function, which is exactly the same as the borrowed function, but (2) this has been fixed in the new function. This is an argument to bind regardless of the call mode. (3) Bind does not call fn

///////////////////////////////////////////////////////////////////

setInterval(fn, 1000); The timer clearInterval ()

///////////////////////////////////////////////////////////////

Any Function is an instance of new Function, and function. prototype is the Function’s prototype

Slice (8, -1) // -1: slice from the back, depending on the number

All functions have both prototype and __proto__ attributes

A instanceof B: Whether A is on the prototype chain of B

/////////////////////////////////////////////////////////////////

Preparsing: Rules for preparsing:

  1. Priority promotion function is variable promotion
  2. If a variable declaration with the same name is encountered, it is ignored

Advice:

  1. Do not declare the same variable twice; subsequent declarations are ignored
  2. Do not declare the same function twice; the latter overwrites the former
  3. Don’t make your functions and variables have the same name (in the same scope)

///////////////////////////////////////////////////////////////

In programming languages:

  1. Block-level scope: JS does not support block-level scope, which consists of curly braces that form the scope ({}) of the code structure. Variables declared in curly braces cannot be accessed externally
  2. Lexical scope in JS, only functions can form scope, so lexical scope is also called function scope

Free variable: A function is a free variable if the variable is not declared inside the function but is used instead

Scope chain: A function can form a scope. If a function is nested within another function, the nested function also has its scope. The chain formed from the function’s scope is called the scope chain.

function fn(num1) { // var num1 = undefined } fn(); // if the function is called with a corporeal parameter, the parameter value is undefined;

///////////////////////////////////////////////////////////////////////////

Recursive function: internal call itself, must have an end condition or it is dead recursion. Recursion uses the idea of reduction to turn a complex problem into a simple one: to turn an unknown into a known, there must be an end condition

///////////////////////////////////////////////////////////////////////////

Cache: A container for storing data (cache) that uses arrays or objects as containers for storing data

The idea of using cache :(rabbit count problem)

  1. First go to the cache container to see if there is any corresponding data
  2. If so, take it out and use it
  3. If not, calculate the result first and store the result in the cache for reuse

Closure conditions: There are two functions that are nested, and the inner function also accesses the declared variables of the outer function.

When a function is called, it creates a space in memory to execute the code inside the function. When the function call ends, the space created by the function is destroyed. The memory footprint of (normal) closures cannot be freed.

What closures do:

  1. Private variables to protect data security
  2. Persist data

Closure problems: Memory occupied by closures will not be freed, causing memory leaks

Memory leak: a block of memory that is occupied and not reclaimed can’t be used by other objects. In terms of reference counting and mark-clear algorithms, the memory used by closures is not recoverable. Don’t abuse closures

Manually free memory used by closures: closure = null;

Reference counting algorithm :js will automatically allocate memory, store objects,0 reference objects, garbage, garbage, garbage collection mechanism –> periodically check for 0 reference objects example: call fn end, should be destroyed in the fn object, but because these objects are not 0 reference objects, can not be collected. Multiple calls to this fn function can cause memory leaks.

The mark-clean algorithm: starts with the window object, cannot find the root object of the window (garbage), circular reference is not a problem, now this method is used to collect garbage. traverse

///////////////////////////////////////////////////////////////////////////

Regular expressions:

Test (): Tests whether a string conforms to the rules of regex

  1. \d ==> numbers 0-9
  2. \D ==> Non-numeric
  3. \w ==> character A-z 0-9 a-z
  4. \W ==> non-character
  5. \s ==> whitespace character
  6. \S ==> non-whitespace characters
  7. Dot (.) Understood as any character