lexical this
This commit is contained in:
@@ -3498,4 +3498,59 @@ return {
|
||||
}
|
||||
},
|
||||
|
||||
// ============================================================================
|
||||
// ARROW FUNCTION LEXICAL THIS
|
||||
// ============================================================================
|
||||
|
||||
test_arrow_captures_this: function() {
|
||||
var obj = {
|
||||
value: 42,
|
||||
getIt: function() {
|
||||
var arrow = () => this.value
|
||||
return arrow()
|
||||
}
|
||||
}
|
||||
if (obj.getIt() != 42) return "arrow should capture enclosing this"
|
||||
},
|
||||
|
||||
test_arrow_captures_this_nested: function() {
|
||||
var obj = {
|
||||
value: 99,
|
||||
getIt: function() {
|
||||
var outer = () => {
|
||||
var inner = () => this.value
|
||||
return inner()
|
||||
}
|
||||
return outer()
|
||||
}
|
||||
}
|
||||
if (obj.getIt() != 99) return "nested arrows should capture enclosing this"
|
||||
},
|
||||
|
||||
test_arrow_this_not_rebound_by_method_call: function() {
|
||||
var obj = {
|
||||
value: 10,
|
||||
getIt: function() {
|
||||
var arrow = () => this.value
|
||||
return arrow()
|
||||
}
|
||||
}
|
||||
var other = { value: 20, stolen: obj.getIt }
|
||||
if (other.stolen() != 20) return "setup failed"
|
||||
if (obj.getIt() != 10) return "arrow this should come from enclosing method"
|
||||
},
|
||||
|
||||
test_arrow_this_with_regular_function_this: function() {
|
||||
var obj = {
|
||||
value: 7,
|
||||
getIt: function() {
|
||||
var arrow = () => this.value
|
||||
var regular = function() { return this }
|
||||
if (regular() != null) return "regular fn direct call should have null this"
|
||||
return arrow()
|
||||
}
|
||||
}
|
||||
if (obj.getIt() != 7) return "arrow should capture this while regular fn does not"
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user