JavaScript language essence - reading notes (2)

Chapter IV Functions

Function is the basic unit functions implemented; a function should be simple functions; programming is to decompose a skill set of requirements into a set of functions and data structures. Avoid implementing a plurality of functions in the internal function.

Use the link function to create a literal Object.prototype, using a function expression creates a function, a link to a Function.prototype, eventually to Object.prototype through the prototype chain link.

After you create a function, it will have a prototype property whose value has a constructor property that is the object of the function.

Function can be called by other parts.

Defined Functions

Four of the function: Function, function name (anonymous function), the function parameters (Parameters, parameter, optional), the function thereof. Function may be an argument to a function or return value. Functions can access the internal variables and parameters, variables and parameters may be accessed outside the parent function. Closure function.

call function

Function during the call, in addition to the incoming parameters (in brackets), passing this further default (four kinds of call mode depending on the value of this function) and arguments (argument). When the argument is greater than the parameter, arguments stores all current function arguments.

arguments can be used in times of uncertainties function parameters (or more parameter) is a pseudo-arguments array, the array does not have a method, which is a limitation.

let sum = function() {
  let sum = 0;
  for (let i = 0; i < arguments.length; i ++) {
    sum += arguemnts[i];
  }
  return sum;
};
Four kinds of call mode function

1, the method call mode

As a method of the object function. this points to this object. By this, the function may be such that the internal object public access methods and properties.

let myObject = {
  value: 0,
  increase = (inc) => {
  	this.value += typeof inc === 'number' ? inc : 1;
	} 
};
// 如果传入的参数是数值,函数的value属性叠加,不是数值就 + 1;
myObject.increase('test');
myObject.increase(2);
console.log(myObject.value === 3);

2, the function call mode

It not as a function of the method is called an object, but as a form of the function is invoked, then this refers to the global variable.

let myObject = {
  value = 3;
}
myObject.double = function () {
  let that = this;
  let helper = function () {
    that.value = add(that.value, that.value);
    console.log(this); //全局变量
  };
  helper(); //以函数的形式调用函数 helper
}
myObject.double();
console.log(myObject.value);

3, the constructor call mode

Create a function using the new constructor, this function is bound to the newly created object. (Creating a new object using the constructor method is early, and now use the class keyword to create js in class). This method is not used in the latest code.

let Quo = function (string) {
  this.status = sy=tring;
}
Quo.prototype.getStatus = function() {
  return this.status;
}
let myQuo = new Quo("test");
myQue.getStatue();

4, Apply call mode: this point to apply the first parameter; the second parameter is a parameter array; (using the comparative method call).

let array = [3, 4];
let sum = add.apply(null, array);
console.log(sum === 7);

let statusObject = {
  status: "OK"
};
let status = Quo.prototype.getStatus.apply(statusObject);
console.log(status === 'OK');

Function encountered return will not continue behind the code.

Exception Handling

Exception: interfere with the normal function during an accident is performed (function numeric parameters need to pass, in fact, the string parameter passed), found that when such an accident, the program needs to throw an exception.

throw statement interrupts execution of the function; the object will throw an exception: a name attribute abnormal abnormal and description of message attributes.

An exception can be captured try-catch. Code generated an exception is captured try statement, the function will jump to catch statement is executed. A try statement can only have a catch-all exception catch (a try only one catch)

If the means to solve the anomaly depends on the type of abnormality, abnormal checker to check for abnormal constitutional name property to determine the type of exception.

function add = (a, b) => {
  if (typeof a !== 'number' || typeof b !== 'number') {
    throw {
      name: 'TypeError',
      message: 'add function needs number'
    };
  }
  return a + b;
}
function tryIt = function() {
  try {
    add("ten", 20);
  }
  catch (e) {
    console.log(e);
  }
}
Recursion

Dom use more recursive tree structure;

let walk_the_dom = fucntion walk (node,func) {
  // 把节点和处理的函数传入,对当前节点进行处理;
  func(node);
  node = node.firstChild;
  while (node) {
    // 遍历每一个子节点,并递归处理
    walk(node, func);
    node = node.nextSibling;
  }
};

let getElementsByAttribute = function (att, value) {
  let results = [];

  walk_the_dom(document.body, fucntion(node) {
    let actual = node.nodeType === 1 && node.getAttribute(att);
    if (typeof actual === 'string' && (actual === value || typeof value !== 'string')) {
      results.push(node);
    }
  });
  return results;
}

Scope

es6 has been block-level scope;

Closure

The need to consolidate

Chapter V inheritance

The need to consolidate

Guess you like

Origin blog.csdn.net/weixin_41697143/article/details/90706246