You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var name = 'jason chuh';
var title = 'Mr.';
var walk = function(){
console.log('我在走路...');
};
console.log('name' in window);
console.log('title' in window);
console.log('walk' in window);
console.log(window['name']);
console.log(window['title']);
console.log(window['walk']);
var 声明
在函数内未使用var声明变量,会自动提升为global object
var func1 = function(){
withoutVar = '我是全局的?';
};
func1();
console.log(withoutVar);
function sum(x, y){ return x+y; }
var sum = function(x, y){ return x+y; }; // var sum = function sum(x,y){ return x+y; };
var sum = new Function('x','y', 'return x+y');
console.log(sum(3,4));
var myName = 'Mac';
function tellMeName(){
var myName = 'PC';
console.log(myName);
}
console.log(myName);
if(true){
myName = 'Pad';
}
console.log(myName);
var countDownFrom = function(num){
console.log(num);
num--;
if(num < 0)
return false;
arguments.callee(num);
};
countDownFrom(5);
arguments
函数的可变长参数
使用arguments.callee得到函数自身
var sum = function(){
var len = arguments.length;
if(0 >= len){
return 0;
}else if(len < 2){
return arguments[0];
} else {
var total = 0;
for(var i = 0; i < len; i++){
total += arguments[i];
}
return total;
}
};
console.log(sum());
console.log(sum(2));
console.log(sum(2,3,4,5));
this对象
运行时确定,可以被更改
针对实例的引用
window是顶层的this
console.log(window === this);
var weight = 1.23;
console.log(weight, window.weight, this.weight);
var playGame = function(){};
console.log(playGame, window.playGame, this.playGame);
// 高阶函数
var callOtherFunction = function(otherFunc){
var testVariable = 123;
otherFunc();
console.log('调用结束');
};
var testVariable = 999;
callOtherFunction(function(){
console.log(testVariable);
});
call/apply
call与apply用于调用函数,同时改变this的定义
call与apply不同在于参数传递方式不同
var Phone = function(){
this.myName = 'Tim';
this.phoneNumber = '13888888888';
this.makeAPhoneCall = function(toName, toPhone){
console.log(this.myName+'('+this.phoneNumber+')打给'+toName+'('+toPhone+')');
};
};
var myPhone = new Phone();
myPhone.makeAPhoneCall('Bill', '020 12345678');
var mike = {myName:'Mike', phoneNumber:'18999999999'};
myPhone.makeAPhoneCall.call(mike, 'Jerry', '10086');
myPhone.makeAPhoneCall.apply(mike, ['Jerry', '10086']);
prototype
对象化的实现方案
prototype chain原型键, instance.property -> (class of instance).prototype.property -> superclass(class of instance).prototype.property -> ... -> object.prototype
instance.construct.prototype == class.prototype
var Fruit = function(){
this.name = 'Fruit';
this.tellMyName = function(){
console.log('My name is ' + this.name);
};
};
Fruit.prototype.color = 'Green';
Fruit.prototype.season = 'Autumn';
var Orange = function(){
this.name = 'Orange';
};
Orange.prototype = new Fruit();
Fruit.prototype.season = 'Summer';
Fruit.prototype.taste = 'Sweet';
var newFruit = new Orange();
newFruit.taste = 'Acid';
newFruit.tellMyName();
console.log(newFruit.color);
console.log(newFruit.season);
console.log(newFruit.taste);
String
concat与Array.join的效率
// #1 使用+连接
var stringWithPlus = '';
for (var i = 0; i < 1000000; i++) {
stringWithPlus += 'hello ';
}
// #2 使用concat方法
var stringWithConcat = '';
for (var i = 0; i < 1000000; i++) {
stringWithConcat.concat('hello ');
}
// #3 使用Array.join
var stringBuffer = [];
for (var i = 0; i < 1000000; i++) {
stringBuffer.push('hello');
}
var stringByBuffer = stringBuffer.join(' ');
// #4 使用underscore模板
var template = "<% _.each(_.range(1000000), function(){ %><%=name%> <% }); %>";
var compiled = _.template(template);
console.log(compiled({name: 'hello'}));
// #5 使用定时器执行, 支持亿次级运算,不会阻止主进程
function concatString(count, str, callback){
var stringPool = [];
setTimeout(function(){
stringPool.push(str);
count--;
if(count > 0){
setTimeout(arguments.callee, 0.001);
} else {
callback(stringPool);
}
}, 0.001);
}
concatString(100000000, 'hello', function(stringPool){
var combined = stringPool.join(" ");
console.log(combined);
});