隐式覆盖坑来了 Shadowing can even occur implicitly in subtle ways, so care must be taken if trying to avoid it. Consider: var anotherObject = { a: 2 }; var myObject = Object.create( anotherObject ); console.log(anotherObject.a); // 2 console.log(myObject.a); // 2 console.log(anotherObject.hasOwnProperty( "a" )); // true console.log(myObject.hasOwnProperty( "a" )); // false myObject.a++; // oops...
2014-11-29 21:55:531人喜欢
隐式覆盖坑来了
Shadowing can even occur implicitly in subtle ways, so care must be taken if trying to avoid it. Consider:引自 Setting and Shadowing Properties
var anotherObject = {
a: 2
};
var myObject = Object.create( anotherObject );
console.log(anotherObject.a); // 2
console.log(myObject.a); // 2
console.log(anotherObject.hasOwnProperty( "a" )); // true
console.log(myObject.hasOwnProperty( "a" )); // false
myObject.a++; // oops, implicit shadowing!
console.log(anotherObject.a); // 2
console.log(myObject.a); // 3
console.log(myObject.hasOwnProperty( "a" )); // true
颤抖吧,this!!! Now, we can summarize the rules for determining this from a function call’s call-site, in their order of precedence. Ask these questions in this order, and stop when the first rule applies. 1. Is the function called with new (new binding)? If so, this is the newly constructed object. var bar = new foo() 2. Is the function called with call or apply (explicit binding), even hidde...
2014-11-20 20:39:211人喜欢
颤抖吧,this!!!
Now, we can summarize the rules for determining this from a function call’s call-site, in their order of precedence. Ask these questions in this order, and stop when the first rule applies.
1. Is the function called with new (new binding)? If so, this is the newly constructed object.
var bar = new foo()
2. Is the function called with call or apply (explicit binding), even hidden inside a bind hard binding? If so, this is the explicitly specified object.
var bar = foo.call( obj2 )
3. Is the function called with a context (implicit binding), otherwise known as an owning or containing object? If so, this is that context object.
var bar = obj1.foo()
4. Otherwise, default the this (default binding). If in strict mode, pick undefined, otherwise pick the global object.
var bar = foo()
That’s it. That’s all it takes to understand the rules of this binding for normal function calls.
We will now examine three scenarios for the myObject.foo = "bar" assignment when foo is not already on myObject directly, but is at a higher level of myObject's [[Prototype]] chain: 1. If a normal data accessor property named foo is found anywhere higher on the [[Prototype]] chain, and it's not marked as read-only (writable:false), then a new property called foo is added directly to myObject, r...
2020-01-04 19:38:59
We will now examine three scenarios for the myObject.foo = "bar" assignment when foo is not already on myObject directly, but is at a higher level of myObject's [[Prototype]] chain:
1. If a normal data accessor property named foo is found anywhere higher on the [[Prototype]] chain, and it's not marked as read-only (writable:false), then a new property called foo is added directly to myObject, resulting in a shadowed property.
2. If a foo is found higher on the [[Prototype]] chain, but it's marked as read-only (writable:false), then both the setting of that existing property as well as the creation of the shadowed property on myObject are disallowed. If the code is running in strict mode, an error will be thrown. Otherwise, the setting of the property value will silently be ignored. Either way, no shadowing occurs.
3. If a foo is found higher on the [[Prototype]] chain and it's a setter, then the setter will always be called. No foo will be added to (aka shadowed on) myObject, nor will the foo setter be redefined.引自第88页
颤抖吧,this!!! Now, we can summarize the rules for determining this from a function call’s call-site, in their order of precedence. Ask these questions in this order, and stop when the first rule applies. 1. Is the function called with new (new binding)? If so, this is the newly constructed object. var bar = new foo() 2. Is the function called with call or apply (explicit binding), even hidde...
2014-11-20 20:39:211人喜欢
颤抖吧,this!!!
Now, we can summarize the rules for determining this from a function call’s call-site, in their order of precedence. Ask these questions in this order, and stop when the first rule applies.
1. Is the function called with new (new binding)? If so, this is the newly constructed object.
var bar = new foo()
2. Is the function called with call or apply (explicit binding), even hidden inside a bind hard binding? If so, this is the explicitly specified object.
var bar = foo.call( obj2 )
3. Is the function called with a context (implicit binding), otherwise known as an owning or containing object? If so, this is that context object.
var bar = obj1.foo()
4. Otherwise, default the this (default binding). If in strict mode, pick undefined, otherwise pick the global object.
var bar = foo()
That’s it. That’s all it takes to understand the rules of this binding for normal function calls.
隐式覆盖坑来了 Shadowing can even occur implicitly in subtle ways, so care must be taken if trying to avoid it. Consider: var anotherObject = { a: 2 }; var myObject = Object.create( anotherObject ); console.log(anotherObject.a); // 2 console.log(myObject.a); // 2 console.log(anotherObject.hasOwnProperty( "a" )); // true console.log(myObject.hasOwnProperty( "a" )); // false myObject.a++; // oops...
2014-11-29 21:55:531人喜欢
隐式覆盖坑来了
Shadowing can even occur implicitly in subtle ways, so care must be taken if trying to avoid it. Consider:引自 Setting and Shadowing Properties
var anotherObject = {
a: 2
};
var myObject = Object.create( anotherObject );
console.log(anotherObject.a); // 2
console.log(myObject.a); // 2
console.log(anotherObject.hasOwnProperty( "a" )); // true
console.log(myObject.hasOwnProperty( "a" )); // false
myObject.a++; // oops, implicit shadowing!
console.log(anotherObject.a); // 2
console.log(myObject.a); // 3
console.log(myObject.hasOwnProperty( "a" )); // true
We will now examine three scenarios for the myObject.foo = "bar" assignment when foo is not already on myObject directly, but is at a higher level of myObject's [[Prototype]] chain: 1. If a normal data accessor property named foo is found anywhere higher on the [[Prototype]] chain, and it's not marked as read-only (writable:false), then a new property called foo is added directly to myObject, r...
2020-01-04 19:38:59
We will now examine three scenarios for the myObject.foo = "bar" assignment when foo is not already on myObject directly, but is at a higher level of myObject's [[Prototype]] chain:
1. If a normal data accessor property named foo is found anywhere higher on the [[Prototype]] chain, and it's not marked as read-only (writable:false), then a new property called foo is added directly to myObject, resulting in a shadowed property.
2. If a foo is found higher on the [[Prototype]] chain, but it's marked as read-only (writable:false), then both the setting of that existing property as well as the creation of the shadowed property on myObject are disallowed. If the code is running in strict mode, an error will be thrown. Otherwise, the setting of the property value will silently be ignored. Either way, no shadowing occurs.
3. If a foo is found higher on the [[Prototype]] chain and it's a setter, then the setter will always be called. No foo will be added to (aka shadowed on) myObject, nor will the foo setter be redefined.引自第88页
隐式覆盖坑来了 Shadowing can even occur implicitly in subtle ways, so care must be taken if trying to avoid it. Consider: var anotherObject = { a: 2 }; var myObject = Object.create( anotherObject ); console.log(anotherObject.a); // 2 console.log(myObject.a); // 2 console.log(anotherObject.hasOwnProperty( "a" )); // true console.log(myObject.hasOwnProperty( "a" )); // false myObject.a++; // oops...
2014-11-29 21:55:531人喜欢
隐式覆盖坑来了
Shadowing can even occur implicitly in subtle ways, so care must be taken if trying to avoid it. Consider:引自 Setting and Shadowing Properties
var anotherObject = {
a: 2
};
var myObject = Object.create( anotherObject );
console.log(anotherObject.a); // 2
console.log(myObject.a); // 2
console.log(anotherObject.hasOwnProperty( "a" )); // true
console.log(myObject.hasOwnProperty( "a" )); // false
myObject.a++; // oops, implicit shadowing!
console.log(anotherObject.a); // 2
console.log(myObject.a); // 3
console.log(myObject.hasOwnProperty( "a" )); // true
颤抖吧,this!!! Now, we can summarize the rules for determining this from a function call’s call-site, in their order of precedence. Ask these questions in this order, and stop when the first rule applies. 1. Is the function called with new (new binding)? If so, this is the newly constructed object. var bar = new foo() 2. Is the function called with call or apply (explicit binding), even hidde...
2014-11-20 20:39:211人喜欢
颤抖吧,this!!!
Now, we can summarize the rules for determining this from a function call’s call-site, in their order of precedence. Ask these questions in this order, and stop when the first rule applies.
1. Is the function called with new (new binding)? If so, this is the newly constructed object.
var bar = new foo()
2. Is the function called with call or apply (explicit binding), even hidden inside a bind hard binding? If so, this is the explicitly specified object.
var bar = foo.call( obj2 )
3. Is the function called with a context (implicit binding), otherwise known as an owning or containing object? If so, this is that context object.
var bar = obj1.foo()
4. Otherwise, default the this (default binding). If in strict mode, pick undefined, otherwise pick the global object.
var bar = foo()
That’s it. That’s all it takes to understand the rules of this binding for normal function calls.
0 有用 胖不墩儿 2018-05-04 14:45:50
花了好多篇幅推销委托模式...我表示,大家用啥我用啥,别让我选。每一次技术选型都是一次极大的消耗。
0 有用 龙三 2014-11-30 17:20:47
this是绑定的context;别瞎几吧乱写类,只用Object.create
0 有用 Roy 2017-04-02 10:56:29
这本书的观点我思考了2周,class,不class,class,不class,最后还是同意composition over inheritance
0 有用 祝我们好运 2016-11-24 20:25:15
依然保持口语化讲干货,解决了我许多从其它书一直遗留下来的疑惑
0 有用 某人 2017-11-27 21:33:59
这本难度有点大,无论是从语言上还是内容上,还是先缓一缓吧😢……
0 有用 秋明 2020-04-16 05:17:36
详细,基础!
0 有用 derjoker 2018-07-02 12:50:00
copies (class) vs. links (prototype)
0 有用 胖不墩儿 2018-05-04 14:45:50
花了好多篇幅推销委托模式...我表示,大家用啥我用啥,别让我选。每一次技术选型都是一次极大的消耗。
0 有用 某人 2017-11-27 21:33:59
这本难度有点大,无论是从语言上还是内容上,还是先缓一缓吧😢……
1 有用 谷幽😷 2017-05-05 09:05:57
JS精要:理解this & prototype