JavaScript基礎知識點
1.陣列拷貝
var arr1=[1,2,3];
var arr2 = arr1.slice();
2.將陣列中元素以某個標示符連線成字串
var items,
messages,
length,
i;
messages = [{
state: 'success',
message: 'This one worked.'
}, {
state: 'success',
message: 'This one worked as well.'
}, {
state: 'error',
message: 'This one did not work.'
}];
length = messages.length;
// bad
function inbox(messages) {
items = '<ul>';
for (i = 0; i < length; i++) {
items += '<li>' + messages[i].message + '</li>';
}
return items + '</ul>';
}
// good
function inbox(messages) {
items = [];
for (i = 0; i < length; i++) {
items[i] = messages[i].message;
}
return '<ul><li>' + items.join('</li><li>') + '</li></ul>';
}
3.變數提升
Variable declarations get hoisted to the top of their scope, their assignment does not.
// we know this wouldn't work (assuming there
// is no notDefined global variable)
function example() {
console.log(notDefined); // => throws a ReferenceError
}
// creating a variable declaration after you
// reference the variable will work due to
// variable hoisting. Note: the assignment
// value of `true` is not hoisted.
function example() {
console.log(declaredButNotAssigned); // => undefined
var declaredButNotAssigned = true;
}
// The interpreter is hoisting the variable
// declaration to the top of the scope.
// Which means our example could be rewritten as:
function example() {
var declaredButNotAssigned;
console.log(declaredButNotAssigned); // => undefined
declaredButNotAssigned = true;
}
Anonymous function expressions hoist their variable name, but not the function assignment.
function example() {
console.log(anonymous); // => undefined
anonymous(); // => TypeError anonymous is not a function
var anonymous = function() {
console.log('anonymous function expression');
};
}
Named function expressions hoist the variable name, not the function name or the function body.
function example() {
console.log(named); // => undefined
named(); // => TypeError named is not a function
superPower(); // => ReferenceError superPower is not defined
var named = function superPower() {
console.log('Flying');
};
}
// the same is true when the function name
// is the same as the variable name.
function example() {
console.log(named); // => undefined
named(); // => TypeError named is not a function
var named = function named() {
console.log('named');
}
}
Function declarations hoist their name and the function body.
function example() {
superPower(); // => Flying
function superPower() {
console.log('Flying');
}
}
相關文章
- JavaScript部分基礎知識點JavaScript
- 前端知識點總結——JavaScript基礎前端JavaScript
- JavaScript的基礎知識點(面試題)JavaScript面試題
- javaScript基礎概念小知識點集JavaScript
- javascript基礎知識JavaScript
- Javascript 基礎知識JavaScript
- javascript事件基礎知識JavaScript事件
- 前端-JavaScript基礎知識前端JavaScript
- java基礎知識點Java
- JavaWeb基礎知識點JavaWeb
- JavaScript 基礎知識入門JavaScript
- JavaScript入門①-基礎知識築基JavaScript
- Java基礎知識點梳理Java
- Servlet基礎知識點整理Servlet
- Java 基礎面試知識點Java面試
- 【轉】JavaScript物件的基礎知識JavaScript物件
- JavaScript基礎知識(Date 的方法)JavaScript
- 【JavaScript的基礎知識總結】JavaScript
- vueX基礎知識點筆記Vue筆記
- Python基礎知識點梳理Python
- ES 基礎知識點總結
- Java基礎知識點總結Java
- Redis 基礎知識點總結Redis
- Dubbo基礎入門知識點
- Java入門基礎知識點Java
- CSS基礎知識點總結CSS
- MySQL基礎知識點彙總MySql
- 必需知道的javaScript基礎知識JavaScript
- 快速掌握JavaScript面試基礎知識(二)JavaScript面試
- 快速掌握JavaScript面試基礎知識(三)JavaScript面試
- JavaScript開發者常忽略或誤用的七個基礎知識點JavaScript
- Java基礎面試知識點總結Java面試
- Flutter 知識點總結-基礎篇Flutter
- JavaSE基礎 (全網最全知識點)Java
- iOS基礎面試知識點總結iOS面試
- 【基礎知識】索引--點陣圖索引索引
- oracle密碼幾點基礎知識Oracle密碼
- Java基礎複習,整理知識點Java