javascript 基础
instanceof
function myInstanceof(obj, constructor) {
if (obj === null || typeof obj !== 'object') {
return false
}
let proto = Object.getPrototypeOf(obj)
while (proto != null) {
if (proto === constructor.prototype) {
return true
}
proto = Object.getPrototypeOf(proto)
}
return false
}
const strObj = new String('Hello');
console.log(strObj instanceof String);
console.log(myInstanceof(strObj, String));
const strObj2 = 'hello';
console.log(strObj2 instanceof String);
console.log(myInstanceof(strObj2, String));
new
function myNew(constructor, ...args) {
let obj = Object.create(constructor.prototype)
const result = constructor.apply(obj, args)
return typeof result === 'object' ? result : obj
}
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHello = function () {
console.log(`Hello, my name is ${this.name} and I'm ${this.age} years old.`);
};
const john = new Person('John', 30)
const john1 = myNew(Person, 'John', 30);
john.sayHello();
john1.sayHello();
call
Function.prototype.myCall = function(context, ...args) {
context = context || window;
context.fn = this;
const result = context.fn(...args);
delete context.fn;
return result;
};
const obj = {
name: 'John'
};
function greet(message) {
console.log(`${message}, ${this.name}`);
}
greet.myCall(obj, 'Hello');
apply
Function.prototype.myApply = function (context, argsArray) {
context = context || window;
context.fn = this;
const result = context.fn(...argsArray);
delete context.fn;
return result;
};
const obj = {
name: 'John'
};
function greet(message) {
console.log(`${message}, ${this.name}`);
}
greet.apply(obj, ['hello'])
greet.myApply(obj, ['Hello']);
bind
Function.prototype.myBind = function(context, ...args) {
const fn = this;
return function(...newArgs) {
const combinedArgs = args.concat(newArgs);
return fn.apply(context, combinedArgs);
};
};
const obj = {
name: 'John'
};
function greet(message) {
console.log(`${message}, ${this.name}`);
}
const boundFn = greet.myBind(obj, 'Hello');
boundFn();
防抖
const debounce = (fn, delay) => {
let timerId = null
return (...args) => {
clearTimeout(timerId)
timerId = setTimeout(() => {
fn(...args)
}, delay);
}
}
柯里化
function add(x, y, z) {
return x + y + z;
}
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args)
} else {
return function (...newArgs) {
return curried.apply(this, args.concat(newArgs))
}
}
}
}
const curriedAdd = curry(add);
const step1 = curriedAdd(2);
const step2 = step1(3);
const result = step2(4);
console.log(result);
console.log(curriedAdd(2)(3, 4));
console.log(curriedAdd(2, 3, 4));
浅拷贝
function shallowCopy(obj) {
if (typeof obj !== 'object' || obj === null) return obj;
const newObj = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = obj[key];
}
}
return newObj;
}
const obj1 = {
name: 'John', age: 30
};
const obj2 = shallowCopy(obj1);
console.log(obj2);
console.log(obj1 === obj2);
obj1.name = 'Bob';
console.log(obj1);
console.log(obj2);
深拷贝
function cloneDeep(obj) {
if (typeof obj !== 'object' || !obj) return obj
let newObj = Array.isArray(obj) ? [] : {}
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
newObj[key] = typeof obj[key] === 'object' ? cloneDeep(obj[key]) : obj[key]
}
}
return newObj;
}