面试够用
/**
* 模仿 new
* @return {[type]} [description]
*/
function createNew() {
var obj = {};
let context = [].shift.call(arguments); // 获取构造函数
obj.__proto__ = context.prototype;
context.apply(obj, [...arguments]);
return obj;
}
// @test
function Person(name) {
this.name = name;
}
var p = createNew(Person, 'zjj');
console.log(p);
// @test
/**
* 模仿实现 instanceof
* @param {[type]} left [左侧参数为一个实例对象]
* @param {[type]} right [右侧为要判断的构造器函数]
* @return {[type]} [true / false]
*/
function instanceOf (left, right) {
let prototype = right.prototype; // 获取目标原型对象
left = left.__proto__;
while (true) {
if(left == null) {
return false;
} else if (left == prototype) {
return true;
}
left = left.__proto__
}
}
/**
* 模仿call
* @param {[type]} context [要绑定的this对象]
* @return {[type]} [返回函数执行结果]
*/
Function.prototype.call_new = function(context) {
var context = context || window;
context.fn = this;
var args = [...arguments].clice(1);
let result = context.fn(...args);
delete context.fn;
return result;
}
// @test
// foo.call_new(obj, 1,2,3)
/**
* 模仿apply
* @param {[type]} context [要绑定的this对象]
* @return {[type]} [执行结果]
*/
Function.prototype.apply_new = function(context) {
var context = context || window;
context.fn = this;
var args = [...arguments][1];
let result;
if(args) {
result = context.fn(...args);
} else {
result = context.fn()
}
delete context.fn;
return result;
}
/**
* 模仿 bind
* @param {[type]} context [要绑定的this对象]
* @return {[type]} [执行结果]
*/
Function.ptototype.bind_new(context) {
var self = this;
var args = [...arguments].slice(1);
return function() {
var args1 = [...arguments].slice(1);
return self.apply(context, args.concat(args1));
}
}
/**
* 函数放抖
* @param {Function} fn [目标函数]
* @param {[type]} delay [延迟]
* @param {[type]} immit [是否第一次触发的时候执行一次]
* @return {[type]} [description]
*/
function debounce(fn, delay, immit) {
var timer;
var firstisOk;
return function() {
var self = this;
var args = arguments;
timer && clearTimeout(timer);
if(immit && !firstisOk) {
fn.apply(self, args);
firstisOk = true;
}
timer = setTimeout(() => {
fn.apply(self, args);
firstisOk = false;
}, delay);
}
}
/**
* 节流
* @param {Function} fn [目标函数]
* @param {[type]} delay [延迟时间]
* @return {[type]} [description]
*/
function throttle(fn, delay) {
let timer,
startTime;
return function() {
let self = this;
let args = arguments;
timer && clearTimeout(timer)
if(!startTime) {
startTime = Date.now();
}
if(Date.now() - startTime > delay) {
fn.apply(self, args)
startTime = Date.now();
} else {
timer = setTimeout(() => {
fn.apply(self, args);
}, delay)
}
}
}
/**
* 深拷贝
* @param {[type]} p [目标对象]
* @param {[type]} c [拷贝后的对象]
* @return {[type]} [description]
*/
function extendsDeeply(p, c) {
var c = c || {};
for(var i in p) {
if(typeof p[i] == 'object') {
c[i] = (p[i] instanceof == Array) ? [] : {}
extendsDeeply(p[i], c[i]);
} else {
c[i] = p[i]
}
}
}
// 手写一个继承
function Parent(name) {
this.name = name;
}
Parent.prototype.getName = function() {
console.log(this.name);
}
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
function create(prototype) {
function F(){};
F.prototype = prototype;
return new F();
}
Child.prototype = create(Parent.ptototype);
Child.prototype.constructor = Child;
Child.prototype.getAge = function() {
console.log(this.age)
}
// 简单实现一个简单的promise
function myPromise(constructor) {
let self = this;
self.status = "pending" //定义状态改变前的初始状态
self.value = undefined; //定义状态为resolved的时候的状态
self.reason = undefined; //定义状态为rejected的时候的状态
function resolve(value) {
if (self.status === "pending") {
self.value = value;
self.status = "resolved";
}
}
function reject(reason) {
if (self.status === "pending") {
self.reason = reason;
self.status = "rejected";
}
}
//捕获构造异常
try {
constructor(resolve, reject);
} catch (e) {
reject(e);
}
}
myPromise.prototype.then = function(onFullfilled, onRejected) {
let self = this;
switch (self.status) {
case "resolved":
onFullfilled(self.value);
break;
case "rejected":
onRejected(self.reason);
break;
default:
}
}
var p = new myPromise(function(resolve, reject) {
resolve(1);
});
p.then((res) => {
console.log(res);
})
// 数组去重
// 手写ajax
var xhr = new XMLHttpRequest();
xhr.open('post', 'http://', true); // 初始化一个请求 xhr.open(method, url, async);
if(xhr.readyState == 4) {
if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
console.log(xhr.responseText);
}
}
}
let postData = {"name1":"value1","name2":"value2"};
postData = (function(value) {
var dataString = "";
for(var key in value){
dataString += key+"="+value[key]+"&";
};
return dataString;
})(postData);
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // 用于设置HTTP请求头,此方法必须在open()方法和send()之间调用
// 异常处理
xhr.onerror = function() {
console.log('Network request failed')
}
// 跨域携带cookie
xhr.withCredentials = true;
xhr.send(postData); // 用于发送HTTP请求,即调用该方法后HTTP请求才会被真正发出,send的参数可以是string和blob
复制代码