JavaScript 是 弱類型 語言,但並非沒有類型,JavaScript能夠識別下面 7 種不一樣類型的值:javascript
可使用 typeof
判斷數據類型,操做符返回一個字符串,但並不是返回的全部結果都符合預期java
typeof false // "boolean" typeof .2 // "number" typeof NaN // "number" typeof '' // "string" typeof undefined // "undefined" typeof Symbol() // "symbol" typeof new Date() // "object" typeof [] // "object" typeof alert // "function" typeof null // "object" typeof not_defined_var // "undefined"
在應用程序中,使用變量來來爲值命名。變量的名稱稱爲 identifiers
es6
var
:函數做用域let
:塊做用域 (block scope local variable)var global_var = 1; function fn () { var fn_var = 2; if(fn_var > 10){ let block_var = 3; global_var2 = 4; } }
只聲明不賦值,變量的默認值是 undefined
數組
const
關鍵字能夠聲明不可變變量,一樣爲塊做用域。對不可變的理解在對象上的理解須要注意瀏覽器
const num = 1; const obj = { prop: 'value' }; num = 2; // Uncaught TypeError: Assignment to constant variable. obj['prop'] = 'value2'; obj = []; // Uncaught TypeError: Assignment to constant variable.
JavaScript中能夠引用稍後聲明的變量,而不會引起異,這一律念稱爲變量聲明提高(hoisting)閉包
console.log(a); // undefined var a = 2;
等同於app
var a; console.log(a); a = 2;
一個函數就是一個能夠被外部代碼調用(或者函數自己遞歸調用)的 子程序
dom
function fn(){} var fn = function(){} var fn = new Function(arg1, arg2, ... argN, funcBody) var fn = (param) => {}
function foo() { return arguments; } foo(1, 2, 3); // Arguments[3] // { "0": 1, "1": 2, "2": 3 }
function foo(...args) { return args; } foo(1, 2, 3); // Array[3] // [1, 2, 3] function fn(a, b, ...args){ return args; } fn(1, 2, 3, 4, 5); // Array[3] // [3, 4, 5]
函數的參數能夠在定義的時候約定默認值ide
function fn (a = 2, b = 3) { return a + b; } fn(2, 3); // 5 fn(2); // 5 fn(); // 5
JavaScript 中對象是可變 鍵控集合
(keyed collections)函數
var obj = { prop: 'value', fn: function(){} }; var date = new Date();
構造函數和普通函數並無區別,使用 new
關鍵字調用就是構造函數,使用構造函數能夠 實例化 一個對象
函數的返回值有兩種可能
return
返回 return
後表達式的求值return
返回 undefined
function People(name, age) { this.name = name; this.age = age; } var people = new People('Byron', 26);
構造函數返回值
前兩種狀況構造函數返回構造對象的實例,實例化對象正是利用的這個特性
第三種構造函數和普通函數表現一致,返回 return
後表達式的結果
prototype
的對象屬性,對象內有一個 constructor
屬性,默認指向函數自己__proto__
的屬性,屬相指向其父類型的 prototype
function Person(name) { this.name = name; } Person.prototype.print = function () { console.log(this.name); }; var p1 = new Person('Byron'); var p2 = new Person('Casper'); p1.print(); p2.print();
做用域能夠通俗的理解
其中我是誰的回答就是 this
馬仔就是個人局部變量
普通函數
undefined
global
window
構造函數:對象的實例
對象方法:對象自己
function isNumber(obj) { return Object.prototype.toString.call(obj) === '[object Number]'; }
bind
返回一個新函數,函數的做用域爲 bind
參數
function fn() { this.i = 0; setInterval(function () { console.log(this.i++); }.bind(this), 500) } fn();
箭頭函數是 ES6 提供的新特性,是簡寫的 函數表達式,擁有詞法做用域和 this
值
function fn() { this.i = 0; setInterval(() => { console.log(this.i++); }, 500) } fn();
在 JavaScript 的場景,繼承有兩個目標,子類須要獲得父類的:
function inherits(child, parent) { var _proptotype = Object.create(parent.prototype); _proptotype.constructor = child.prototype.constructor; child.prototype = _proptotype; } function People(name, age) { this.name = name; this.age = age; } People.prototype.getName = function () { return this.name; } function English(name, age, language) { People.call(this, name, age); this.language = language; } inherits(English, People); English.prototype.introduce = function () { console.log('Hi, I am ' + this.getName()); console.log('I speak ' + this.language); } function Chinese(name, age, language) { People.call(this, name, age); this.language = language; } inherits(Chinese, People); Chinese.prototype.introduce = function () { console.log('你好,我是' + this.getName()); console.log('我說' + this.language); } var en = new English('Byron', 26, 'English'); var cn = new Chinese('色拉油', 27, '漢語'); en.introduce(); cn.introduce();
"use strict"; class People{ constructor(name, age){ this.name = name; this.age = age; } getName(){ return this.name; } } class English extends People{ constructor(name, age, language){ super(name, age); this.language = language; } introduce(){ console.log('Hi, I am ' + this.getName()); console.log('I speak ' + this.language); } } let en = new English('Byron', 26, 'English'); en.introduce();
loop: for (var i = 0; i < 10; i++) { for (var j = 0; j < 5; j++) { console.log(j); if (j === 1) { break loop; } } } console.log(i);
var x = { a:1 }; { a:1 } { a:1, b:2 }
( function() {}() ); ( function() {} )(); [ function() {}() ]; ~ function() {}(); ! function() {}(); + function() {}(); - function() {}(); delete function() {}(); typeof function() {}(); void function() {}(); new function() {}(); new function() {}; var f = function() {}(); 1, function() {}(); 1 ^ function() {}(); 1 > function() {}();
高階函數是把函數當作參數或者返回值是函數的函數
[1, 2, 3, 4].forEach(function(item){ console.log(item); });
閉包由兩部分組成
function makeCounter(init) { var init = init || 0; return function(){ return ++init; } } var counter = makeCounter(10); console.log(counter()); console.log(counter());
for (var i = 0; i < doms.length; i++) { doms.eq(i).on('click', function (ev) { console.log(i); }); }
for (var i = 0; i < doms.length; i++) { (function (i) { doms.eq(i).on('click', function (ev) { console.log(i); }); })(i); }
function eventBinderGenerator() { if (window.addEventListener) { return function (element, type, handler) { element.addEventListener(type, hanlder, false); } } else { return function (element, type, handler) { element.attachEvent('on' + type, handler.bind(element, window.event)); } } } var addEvent = eventBinderGenerator();
一種容許使用部分參數生成函數的方式
function isType(type) { return function(obj){ return Object.prototype.toString.call(obj) === '[object '+ type +']'; } } var isNumber = isType('Number'); console.log(isNumber(1)); console.log(isNumber('s')); var isArray = isType('Array'); console.log(isArray(1)); console.log(isArray([1, 2, 3]));
function f(n) { return n * n; } function g(n) { return n * 2; } console.log(f(g(5))); function pipe(f, g) { return function () { return f.call(null, g.apply(null, arguments)); } } var fn = pipe(f, g); console.log(fn(5));
遞歸很容易發生"棧溢出"錯誤(stack overflow)
function factorial(n) { if (n === 1) return 1; return n * factorial(n - 1); } factorial(5) // 120
但對於尾遞歸來講,因爲只存在一個調用記錄,因此永遠不會發生"棧溢出"錯誤
function factorial(n, total) { if (n === 1) return total; return factorial(n - 1, n * total); } factorial(5, 1) // 120
柯里化減小參數
function currying(fn, n) { return function (m) { return fn.call(this, m, n); }; } function tailFactorial(n, total) { if (n === 1) return total; return tailFactorial(n - 1, n * total); } const factorial = currying(tailFactorial, 1); factorial(5) // 120
Function.prototype.uncurry = function () { return this.call.bind(this); };
push 通用化
var push = Array.prototype.push.uncurry(); var arr = []; push(arr, 1); push(arr, 2); push(arr, 3); console.log(arr);