This is the 8th day of my participation in Gwen Challenge

The function

ES5 extends the bind method for functions

Function: bind the scope to the function (when the function is executed, change the scope of the function and pass parameters)

The difference between call and apply

They’re all methods that change the scope of a function, that execute the function and change the scope when the method is called, and the first argument is the changed scope

Call starts with the second argument, which represents the argument passed to the function

The apply second argument is an array, with each member representing the argument passed to the function

Bind is similar to call

The first parameter represents the changed scope object

Starting with the second parameter, the parameter is passed

The difference between:

Call | apply call is executed

The bind call does not execute, but gets a new method that can be executed

The date to expand

ToJSON converts dates toJSON format, (standardized format) example:

var date = new Date(a);console.log(date)
console.log(date.toJSON())
Copy the code

Strict mode

ES5 extends the strict model to make our code more secure and reliable

The code that was written before was in normal mode

In normal mode, it is possible to define a variable without using var. In strict mode, it is not allowed

Octal is allowed in normal mode, but not in strict mode

Arguments. callee can be used for normal touch, but not for strict mode

Turn on strict mode

Usage: “Use strict”

When “Use Strict” is added, browsers that know it will automatically turn on strict mode, and those that don’t will just

As a string definition, does not affect subsequent code

Once strict mode is turned on, code must be written according to strict mode’s code specification

Add “use strict” to the first line of the JS code, the code will be in “global strict mode”

Add “use strict” to the first line of a function. When the function executes, the function will be in “local strict mode”.

Example: In normal mode:

// Use var to define variables
var a = 10;

// Not using val is not allowed in strict mode
b = 20; 
Copy the code

Results:

In strict mode:

// Enable Strict mode Until strict mode code is enabled, it is still in normal mode
"use strict"
// Use var to define variables
var a = 10;

// Not using val is not allowed in strict mode
b = 20; 
Copy the code

Error:

For example:

// Local strict mode
// Define the function
function localStrict() {
	// Enable strict mode
	"use strict"
	// Define variables
	var a = 10;
	// b = 11;
}

localStrict();


// Do not use var to define variables
c = 20;
Copy the code

Define variables in strict mode

Variables must be defined with a val declaration under strict conditions

Allow:

var a = 10
Copy the code

Do not allow:

b = 20
Copy the code

For example:

// Enable strict mode
"use strict"
// Use var to define variables
var a = 10;

// Do not use var to define variables
// It is not allowed to define variables without var in strict mode
b = 20;
Copy the code

octal

There are three kinds of base system in JS, which are base 8, base 10, and base 16

The default is base 10

Starting with 0 is base 8

Starting with 0x and ox is hexadecimal

Allow:

var num = 16;var num1 = 0xff
Copy the code

Do not allow:

var num = 011;
Copy the code

For example:

In normal mode:

// Define variables
var num = 16;
var num1 = 0xff;

var num2 = 011;
Copy the code

Output:

In strict mode:

// Enable strict mode
"use strict"
var num2 = 011;
Copy the code

An error:

arguments.callee

The more “specific” your code is, the better it will be

Arguments.callee cannot be resolved at compile time and can only be determined at execution time, so there is no performance improvement

In normal mode:

/ / security
function People(name, age, sex) {
	// Determine if this points to
	if (this instanceof arguments.callee) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	} else {
		// No new call is used
		return new arguments.callee(name, age, sex); }}// instantiate the object
var p = new People("Xiao Ming".12."Male");
var p1 = People("Small dragon".12."Male");
Copy the code

Output:

In strict mode:

// Enable strict mode
"use strict"
/ / security
function People(name, age, sex) {
	// Determine if this points to
	if (this instanceof arguments.callee) {
		this.name = name;
		this.age = age;
		this.sex = sex;
	} else {
		// No new call is used
		return new arguments.callee(name, age, sex); }}// instantiate the object
var p = new People("Xiao Ming".12."Male");
var p1 = People("Small dragon".12."Male");
Copy the code

An error:

delete

Use delete to delete properties from an object

If you want to delete a variable

In normal mode, the deletion succeeds but no error is reported. In strict mode, an error is reported

In normal mode:

// Define global variables
var a = 10;
window.b = 20;

// Delete variables
6console.log(delete a);
console.log(delete b);
Copy the code

Output:

In strict mode:

// Enable strict mode
"use strict"
// Define global variables
var a = 10;
window.b = 20;

// Delete variables
console.log(delete a);
console.log(delete b);
Copy the code

An error:

Use reserved words to define variables

In normal mode you can define variables using reserved words

Strict mode is not allowed

In normal mode:

// Use reserved words
var public = "hehe";
Copy the code

Output:

In strict mode:

// Enable strict mode
"use strict"
// Use reserved words
var public = "hehe";
Copy the code

An error:

eval

Eval is a function that executes a string as code, but contaminates the global scope

This is not allowed in strict mode, so you can avoid contaminating the global scope

For example:

In normal mode:

// Define variables
var strcode = "var a = 10";

// eval takes one argument as a string, and the result is the string code
When eval is executed, a is actually defined
eval(strcode);
Copy the code

Output:

In strict mode:

// Enable strict mode
"use strict"
// Define variables
var strcode = "var a = 10";

// eval takes one argument as a string, and the result is the string code
When eval is executed, a is actually defined
eval(strcode);
Copy the code

Results: