var {log} = console;
var obj1 = {};
var obj2 = new Object();
console.log(Object.getPrototypeOf(obj1) === Object.prototype);
console.log(Object.getPrototypeOf(obj2) === Object.prototype);
function Person(name){
this.name = name;
}
Person.prototype.getName = function(){
return this.name;
}
var p = new Person('lucy');
console.log(p.name);
console.log(p.getName());
console.log(Object.getPrototypeOf(p) === Person.prototype);
console.log(Object.getPrototypeOf(p) === Object.prototype)
var p1 = {
name:"sevn",
getName:function(){
return this.name;
}
}
var p2 = {
name:"anne"
}
console.log(p1.getName());
console.log(p1.getName.call(p2))
var fun = function(a,b,c){
console.log([a,b,c]);
}
fun.apply(null,[1,2,3])
console.log(Math.max.apply( null, [ 1, 2, 5, 3, 4 ] ))
console.log(Math.max(1, 2, 5, 3, 4))
function mult(){
var a = 1;
for(var i=0,l=arguments.length;i<l;i++){
a = a * arguments[i];
}
return a;
}
console.log(mult(1,1,2,3))
var cache = {};
function multCache(){
var args = Array.prototype.join.call(arguments,',');
if(cache[args]){
return cache[args];
}
var a = 1;
for(var i = 0,l = arguments.length; i < l;i++){
a *= arguments[i];
}
return cache[args] = a;
}
log(multCache(6,6))
var extend1 = function(){
var value = 0;
return {
call:function(){
value++;
console.log(value)
}
}
}
var extend1 = extend1();
extend1.call();
extend1.call();
var extend2 = {
value:0,
call:function(){
this.value++;
console.log(this.value);
}
}
extend2.call();
extend2.call();
var Extend3 = function(){
this.value = 0;
}
Extend3.prototype.call = function(){
this.value++;
console.log(this.value)
}
var extend3 = new Extend3();
extend3.call();
let res1 = [2,4,3].sort(function(a,b){
return a-b;
})
log(res1);
let res2 = [2,4,3].sort(function(a,b){
return b-a;
})
log(res2);
var isString = function(obj){
return Object.prototype.toString.call(obj) === '[object String]'
}
var isArray = function(obj){
return Object.prototype.toString.call(obj) === '[object Array]';
}
var isNumber = function(obj){
return Object.prototype.toString.call(obj) === '[object Number]'
}
log(isString('1'));
log(isArray([1,2,3]));
log(isNumber(2))
function isType(type){
return function(obj){
return Object.prototype.toString.call(obj) === `[object ${type}]`
}
}
var isString1 = isType('String');
var isArray1 = isType('Array');
var isNumber1 = isType('Number');
log(isString1('1'));
log(isArray1([1,2,3]));
log(isNumber1(2))
// 批量注册
var Type = {};
for(var i = 0, type; type = [ 'String', 'Array', 'Number' ][ i++ ];){
(function(type){
log(type,'---------type')
Type['is'+type] = function(obj){
console.log(type,'----------functiontype')
return Object.prototype.toString.call(obj) === `[object ${type}]`
}
})(type)
}
log(Type.isString(1))
log(Type.isNumber(1))
// 函数节流实现
// 在一段时间内只触发一次
// 非用户直接控制,函数频繁调用优化
log("====================================")
var throttle = function(fn,interval){
var _self = fn, // 保存需要延迟执行的函数索引
timer,// 定时器
firstTime = true;
return function(){
var _me = this,
args = arguments;
if(firstTime){
_self.apply(_me,args);
return firstTime = false;
}
if(timer){
// 如果timer存在,说明上一次的定时器还没执行完成
return false
}
timer = setTimeout(function(){
clearTimeout();
timer = null;
_self.apply(_me,args)
},interval || 500)
}
}
window.onresize = throttle(function(){
console.log(1)
},500)
console