Home  >  Article  >  Web Front-end  >  js-notes

js-notes

不言
不言Original
2018-04-26 14:31:201258browse

This article will share with you some js notes. Friends who are interested can take a look.

1. JS will be pre-compiled once;

var a; 
if (!(“a” in window)) { 
   a = 1; 
}

alert (a); // undefined
In this way, the meaning of the question is very clear: first declare a, and then determine whether a exists. If it does not exist, assign it to 1.
It is obvious that a will always exist in the window , this assignment statement will never be executed, so the result is undefined.

2. The function declaration will overwrite the variable declaration, but it will not overwrite the variable assignment. See chestnuts

function value(){ 
   return 1; 
} 
alert(typeof value);    //”function”

As soon as possible, the variable declaration is defined below, but the variable value is still function , that is to say, in this case, the priority of
function declaration is higher than the priority of variable declaration, but if the variable value is assigned, the result will be completely different:

function value(){ 
   return 1; 
} 
var value = 1; 
alert(typeof value);    //”number”

After the value is assigned, the variable assignment initialization overwrites the function declaration.

Shallow copy only copies the basic type of data. For arrays or objects, only its memory address is copied, so there is a possibility that the meta-object may be tampered with;

function copy(p) { 
   let c = {}; 
   for(let i in p) { 
       c[i] = p[i]; 
   } 
   return c; 
}

1. JS will be pre-compiled;

var a; 
if (!(“a” in window)) { 
   a = 1; 
} 
alert(a); // undefined

In this way, the meaning of the question is very clear: first declare a, and then determine whether a exists. If it does not exist, assign it to 1.
Obviously a will always exist in the window. This assignment statement will never be executed, so the result is undefined.

2. The function declaration will overwrite the variable declaration, but it will not overwrite the variable assignment. See chestnuts

function value(){ 
   return 1; 
} 
alert(typeof value);    //”function”

As soon as possible, the variable declaration is defined below, but the variable value is still function , that is to say, in this case, the priority of
function declaration is higher than the priority of variable declaration, but if the variable value is assigned, the result will be completely different:

function value(){ 
   return 1; 
} 
var value = 1; 
alert(typeof value);    //”number”


After the value is assigned, the variable assignment initialization overwrites the function declaration.

Shallow copy only copies the basic type of data. For arrays or objects, only its memory address is copied, so there is a possibility that the meta-object may be tampered with;

function copy(p) { 
   let c = {}; 
   for(let i in p) { 
       c[i] = p[i]; 
   } 
   return c; 
}

Related recommendations:

Another way of writing js to share

Detailed explanation of js implementation of fuzzy query examples

The above is the detailed content of js-notes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn