
您可以在 github 仓库中找到这篇文章中的所有代码。
背包客(Backpacker),又称驴友,泛指三五成群或者单枪匹马四处游逛的人,也就是背着背包做长途自助旅行的人,现主要是以群体登山、徒步、探险等寻找刺激的人为主,目的在于通过游历认识世界,认识自我,挑战极限等。[1] 驴友一般喜结伴出行,有的准备有帐篷、睡袋,露宿在山间旷野。另一种说法是,取自"旅友"的谐音,即旅行之友的意思。台湾多称呼他们为自助旅行玩家或自助旅游者,香港
43
/**
* @param {any} obj
* @param {target} target
* @return {boolean}
*/
// one-line solution
function myinstanceof(obj, fn) {
return fn.prototype.isprototypeof(obj);
}
function myinstanceof(obj, fn) {
if (typeof obj !== "object" || obj === null) {
return false;
}
if (typeof fn !== "function") {
return false;
}
let proto = object.getprototypeof(obj);
while (proto) {
if (proto === fn.prototype) {
return true;
}
proto = object.getprototypeof(proto);
}
return false;
}
// usage example
class a {}
class b extends a {}
const b = new b();
console.log(myinstanceof(b, b)); // => true
console.log(myinstanceof(b, a)); // => true
console.log(myinstanceof(b, object)); // => true
function c() {}
console.log(myinstanceof(b, c)); // => false
c.prototype = b.prototype;
console.log(myinstanceof(b, c)); // => true
c.prototype = {};
console.log(myinstanceof(b, c)); // => false
/**
* @param {Function} constructor
* @param {any[]} args
* `myNew(constructor, ...args)` should return the same as `new constructor(...args)`
*/
function myNew(constructor, ...args) {
const obj = {};
Object.setPrototypeOf(obj, constructor.prototype);
const result = constructor.call(obj, ...args);
if (typeof result !== "object" || result == null) {
return obj;
} else {
return result;
}
}
// Usage example
function Person(name) {
this.name = name;
}
const person = myNew(Person, "Mike");
console.log(person); // => Person { name: 'Mike' }
以上就是OOP - JavaScript 挑战的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号