洪名辰
Full Stack Developer
{
"name": "洪名辰",
"gender": "男",
"position": "前端工程師",
"skill": {
"Front-End":{
"basic": ["HTML", "CSS", "Javascript", "SEO"],
"JS-framework": ["AngularJS", "ReactJS"],
"CSS-extend": ["Sass", "Susy"],
"tools":["Sublime", "Gulp"]
},
"Back-End":{
"basic": ["C#", "Javascript"],
"framework": ["Asp.Net MVC", "NodeJS"],
"tools":["Visual Studio", "Sublime"]
}
}
}
- Douglas Crockford
var car = {};
car.name = 'BMW';
console.log(car.name); //BMW
console.log(car['name']); //BMW
var demo = function(){
return "Hello World";
}
demo.qqqq = 'qqqq';
console.log(demo.qqqq); //qqqq
var a = "aaa";
a.name = "a"; // 沒有效果
console.log(a.name); // undefined
var car = {};
typeof(car); //object
car = "car";
typeof(car); //string
car = 1;
typeof(car); //number
圖片來源:http://goo.gl/2dsj4f
var car;
car = {}; //literal notation
car.name = "BMW";
/**
*
* anti-pattern
*
**/
var car2;
car2 = new Object();
car2.name = new String("BMW");
var a = 10;
/*
* 應避免使用
*/
var a = new Number(10);
var a = Number('10'); //10,轉型
var a = Number('10aa'); //轉型失敗 --->NaN
var a = +"10"; //10
/*
* 常見的使用技巧
* 避免出現NaN,請使用parseInt、parseFloat。
* 務必指定基數
*/
var a = parseInt('10aa', 10); //10,字串解析
Number 內建屬性及方法說明:https://goo.gl/3GfWl8
var a = 'Jerry';
/*
* 應避免使用
*/
var a = new String('Jerry'); // String {0: 'J', 1: 'e', 2: 'r', 3: 'r', 4: 'y' }
var a = String('Jerry');
/*
* 常見的使用技巧
*/
var b = a.length; // 5,length可取得字串長度
var c = a[0]; // "J",取得第一個字元
String 內建屬性及方法說明:https://goo.gl/xErvit
var a = true;
/*
* 應避免使用
*/
var a = new Boolean(false);
var a = Boolean('false');
var a = Boolean('0');
var a = Boolean(0);
Boolean 內建屬性及方法說明:https://goo.gl/CEmIH6
function(str, num){
var str = str || 'Jerry';
var num = num || 100;
}
var a = [];
/*
* 應避免使用
*/
var a = new Array();
/*
* 常用技巧
*/
a[0] = "Jerry";
a[1] = "Anna";
a.push("Ray");
a.indexOf("Jerry"); //0
a.splice(0,1);
Array 內建屬性及方法說明:https://goo.gl/aeeZI
var a = new Date();
var a = new Date(10000);
var a = new Date('2015-02-21');
Date 內建屬性及方法說明:https://goo.gl/DaGfQ
圖片來源:http://goo.gl/4Lyjc
//物件
window.document.form[0];
//宣告變數a
var a = window.document.form[0];
//型別
typeof(a);
//重新賦值
a = '123';
a == window.document.form[0]; //??
//宣告物件
window.document.jj = { name:"Jerry"}
//宣告變數a
var a = window.document.jj;
//型別
typeof(a);
//重新賦值
a.name = '123';
a == window.document.jj; //??
var a = "Jerry"; //全域變數
var b = function(){
console.log(a);
var a = "Ray"; //區域變數
console.log(a);
}
b(); //會印出什麼?
var a = "Jerry"; //全域變數
var b = function(){
var a;
console.log(a);
a = "Ray"; //區域變數
console.log(a);
}
b();
// undefined
// "Lin"
/*
* 第一種寫法,JSHint推薦寫法
*/
(function(a, b){
var c;
// ...
}(a, b));
/*
* 第二種寫法,注意右括號
*/
(function(a, b){
var c;
// ...
})(a, b);
var findNodes = function(){
var i = 1000000,
nodes = [],
found;
while(i){
i = i-1;
// 這裡是很複雜的邏輯...
nodes.push(found);
}
return nodes;
}
var hide = function(nodes){
var i = 0,
max = nodes.length;
for(i = 0;i < max; i+=1)
{
nodes[i].style.display = "none";
}
}
//執行
hide(findNodes());
var findNodes = function(callback){
var i = 1000000,
nodes = [],
found;
while(i){
i = i-1;
// 這裡是很複雜的邏輯...
//執行回乎
if(typeof callback === "function")
{
callback(found);
}
nodes.push(found);
}
return nodes;
}
// 使用匿名函式,就不用定義hide
findNodes(function(node){
node.style.display = "none";
});
閉包的特性
在特定函式中存取另一個函式的變數
外層函式宣告的變數可以在內層函式中使用
重點觀念:重用域連結 (Scope Chain)
閉包的程式碼外觀
運用在巢狀函式的定義中
var a = function(){
var x = 100;
return function(){
x = x - 10;
return x;
}
}
var b = a();
b(); //90
b(); //80
for(var i=0;i<10;i++){
setTimeout(function(){
console.log(i);
}, 0);
// console.log(i);
}
//使用立即函式 和 閉包特性,來達到
for(var i=0;i<10;i++){
(function(){
var j = i;
setTimeout(function(){
console.log(j);
}, 0);
}());
}
By 洪名辰