Home > Web Front-end > JS Tutorial > body text

js-notes

不言
Release: 2018-04-26 14:31:20
Original
1284 people have browsed it

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; 
}
Copy after login

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”
Copy after login
Copy after login

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”
Copy after login
Copy after login

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; 
}
Copy after login
Copy after login

1. JS will be pre-compiled;

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

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”
Copy after login
Copy after login

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”
Copy after login
Copy after login


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; 
}
Copy after login
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!