-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
day1 #1
Comments
先说下什么是原型,每个js对象创建时,都会通过属性__proto__关联另一个对象,这个对象是其构造函数的原型。(例如:var obj = {}, obj.proto === Object.prototype)
寄生组合式继承,原理是通过创建一个中间函数继承父级的原型,实例化后抛出,修改此原型的constructor指向为子级,并赋值给子级的原型。 function object (o) {
function F () {}
F.prototype = o
return new F()
}
function extend(child, parent) {
var prototype = object(parent.prototype)
prototype.constructor = child
child.prototype = prototype
}
// 使用
extend(Child, Parent) 这种继承方式避免了父级函数原型的污染,保持原型链的完整,同时子级实例化时不会多次创建方法
作用域是js上下文中的概念,js的上下文分为全局上下文和函数上下文。
页面中会频繁触发的事件,resize、scroll、mousemove等等,会在事件停止触发后延迟 n 秒再执行。 function debounce(dn, delay, immediate) {
var timer
return function () {
var context = this
var args = arguments
if(timer)clearTimeout(timer)
if (immediate) {
var startNow = !timer
startNow && fn.apply(context, args) // 立即执行
timer = setTimeout(() => {
timer = null
}, delay)
} else {
timer = setTimeout(function () {
fn.apply(context, args)
}, delay)
}
}
}
function fib(n) {
if (n < 2) return n
let x = 0, y = 0, z = 1
for(let i = 2; i <= n; i++) {
x = y
y = z
z = x + y
}
return z
} |
每个对象都会有自己的prototype,当查找自己的属性或者方法找不到时,会查找_proto_里的属性和方法,这个proto是它父级的prototype,就这样一层一层往上找 就是原型链 |
知识点
编程题
算法题
The text was updated successfully, but these errors were encountered: