js Advanced Course Reading Notes Chapter 5 - Reference Types (5.6)

Internal attributes of functions

There are two special objects in the function.

  • arguments: It contains all the parameters, and the important thing is that it has an attribute called callee, which is used to point to a function that has this parameter. What are the benefits?

Example:

function fa(num){
  if(num<=1){
    return 1;
  }else{
    return num*fa(num-1);
  }

}
alert(fa(3));//6

This is a normal recursive call, but highly coupled, i.e., heavily dependent on fa ().
But by using arguments.cellee, you can change the name of the function at will.

Example:

function fa(num){
  if(num<=1){
    return 1;
  }else{
    return num*arguments.callee(num-1);
  }

}

var fal = fa;  //Random change of function name
alert(fa(3));//6
alert(fal(3));//6

This is because callee itself is a pointer.

  • This: and the meaning of this in java and c has been roughly the same, meaning the object currently referred to.

We use examples to explain:

window.color = 'red';
var o = {color:'blue'};

function sayColor(){
  alert(this.color);
}


sayColor(); //red
o.sayColor = sayColor; //A function name without () is a pointer. Here, give the pointer to 0.sayColor.
o.sayColor();  //blue

SayColor functions and is global, while in js, the global is referred to by window s, so it is red that calls sayColor directly.
Second, it's very important because in js, the function name itself is a pointer, so whoever points to it is the same.

  • caller, another property of the function object, is used to save the reference (call) of the current function.

The following are examples:

function outer(){
  inner();
}

function inner(){
  alert(inner.caller);
}

outer();

Output:

function outer(){
  inner();
}

That is the reference of inner.
You learned arguments.callee before, which can also be used here to make the coupling looser.

function outer(){
  inner();
}

function inner(){
  alert(arguments.callee.caller);
}

outer();

Attributes and Methods of Functions

Since a function is an object, it has attributes and methods. The general attributes are length and prototype.

length denotes the number of parameters that a function receives:

function add(num1,num2){
  return num1+num2;
}

alert(add.length);  //Two parameters

prototype in Chapter 6

apply () and call ()

apply example:

function sum(num1,num2){
  return num1+num2;
}

function callSum(num1,num2){
  return sum.apply(this,arguments);  //this is the current scope (window, because it is called in the global scope)
}
function callSum2(num1,num2){
  return sum.apply(this,[num1,num2]);
}

alert(callSum(10,10));  //20
alert(callSum(10,10));  //20

The only difference of call () is that except for the first this parameter, the other transfer parameters must be listed one by one.

function sum(num1,num2){
  return num1+num2;
}

function callSum(num1,num2){
  return sum.call(this,num1,num2);  
}

alert(callSum(10,10)); //20

Actually, the real function of apply and call is to extend the scope of function.
Example:

window.color = 'red';
var o = {color:'blue'};

function sayColor(){
 alert(this.color); 
}

sayColor.call(this);//red
sayColor.call(window);//red
sayColor.call(o);//blue

From above, sayColor was originally a globally defined function, but it used call, in the third call, to make the result blue.

bind()

Bind is used to create instance functions and bind the value of this to bind functions.
Example:

window.color = 'red';
var o = {color:'blue'};

function sayColor(){
 alert(this.color); 
}

var newSayColor = sayColor.bind(o);
newSayColor();//blue

Basic Packaging Types

We know that basic types don't have methods, but we're glad that ecmascript provides basic types of packaging.
What does it mean: When accessing a basic type, an instance of the basic type is created, the method is invoked on the strength, and the instance is finally eliminated. This makes the basic type the same as a string.

The difference between the reference type and the basic type is the lifetime. The reference type will always be in memory, while the basic type wrapper will be destroyed at the moment of execution.

Keywords: Attribute Java ECMAScript

Added by usamaalam on Thu, 06 Jun 2019 21:17:00 +0300