cheungkin24 / notes Goto Github PK
View Code? Open in Web Editor NEW随手记录
随手记录
在JavaScript里,想判断一个变量是什么类型时,会使用typeof,在使用typeof时无论是什么类型的对象,都只会返回object,instanceof的作用与typeof相似,且instanceof方法可以返回对象究竟是什么对象。
以下为instanceof的JavaScript实现代码
function instance_of(L, R) {
R = R.prototype
L = L.__proto__
while(true) {
if (L === null) {
return false
}
if (L === R) {
return true
}
L === L.__proto__
}
}
构造函数都有一个prototype属性指向自己的原型对象,而通过构造函数生成的实例就有一个属性__proto__指向这个原型对象
用 Function.prototype.bind 创建的函数对象没有 prototype 属性
Function.prototype === Function.[[proto]]
typeof Function.prototype // function
function Parent () {}
function Child () {}
Child.prototype === new Parent()
const child = new Child()
child instanceof Parent //true
child instanceof Child //true
// 第一次循环
L === Object.__proto__ // 也就是等于Function.prototype
R === Object.prototype // 也就是等于Object.prototype
第二次循环
L === Function.prototype.__proto__ // 也就是等于Object.prototype
R === Object.prototype
// 结果为true
// 第一次循环
L === Function.__proto__ // 相当于Function.prototype
R === Function.prototype
// 结果为true,因为Function.__proto__ === Function.prototype结果为true
// 第一次循环
L === Number.__proto__ // 相当于Function.prototype
R === Number.prototype
// 第二次循环
L === Function.prototype.__proto__ // 相当于Object.prototype
// 第三次循环
L === Object.prototype.__proto__ // null
// 结果为false
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.