activeExecutionContext = {
VO: {...},
this: thisValue
};
// 显示定义全局对象的属性
this.a = 10; // global.a = 10
alert(a); // 10
// 通过赋值给一个无标示符隐式
b = 20;
alert(this.b); // 20
// 也是通过变量声明隐式声明的
// 因为全局上下文的变量对象是全局对象自身
var c = 30;
alert(this.c); // 30
var foo = {x: 10};
var bar = {
x: 20,
test: function () {
alert(this === bar); // true
alert(this.x); // 20
this = foo; // 错误,任何时候不能改变this的值
alert(this.x); // 如果不出错的话,应该是10,而不是20
}
};
// 在进入上下文的时候
// this被当成bar对象
// determined as "bar" object; why so - will
// be discussed below in detail
bar.test(); // true, 20
foo.test = bar.test;
// 不过,这里this依然不会是foo
// 尽管调用的是相同的function
foo.test(); // false, 10
function foo() {
alert(this);
}
foo(); // global
alert(foo === foo.prototype.constructor); // true
// 但是同一个function的不同的调用表达式,this是不同的
foo.prototype.constructor(); // foo.prototype
var foo = {
bar: function () {
alert(this);
alert(this === foo);
}
};
foo.bar(); // foo, true
var exampleFunc = foo.bar;
alert(exampleFunc === foo.bar); // true
// 再一次,同一个function的不同的调用表达式,this是不同的
exampleFunc(); // global, false
var valueOfReferenceType = {
base: <base object>,
propertyName: <property name>
};
var foo = 10;
function bar() {}
var fooReference = {
base: global,
propertyName: 'foo'
};
var barReference = {
base: global,
propertyName: 'bar'
};
function GetValue(value) {
if (Type(value) != Reference) {
return value;
}
var base = GetBase(value);
if (base === null) {
throw new ReferenceError;
}
return base.[[Get]](GetPropertyName(value));
}
GetValue(fooReference); // 10
GetValue(barReference); // function object "bar"
foo.bar();
foo['bar']();
var fooBarReference = {
base: foo,
propertyName: 'bar'
};
GetValue(fooBarReference); // function object "bar"
function foo() {
return this;
}
foo(); // global
var fooReference = {
base: global,
propertyName: 'foo'
};
var foo = {
bar: function () {
return this;
}
};
foo.bar(); // foo
var fooBarReference = {
base: foo,
propertyName: 'bar'
};
var test = foo.bar;
test(); // global
var testReference = {
base: global,
propertyName: 'test'
};
function foo() {
alert(this);
}
foo(); // global, because
var fooReference = {
base: global,
propertyName: 'foo'
};
alert(foo === foo.prototype.constructor); // true
// 另外一种形式的调用表达式
foo.prototype.constructor(); // foo.prototype, because
var fooPrototypeConstructorReference = {
base: foo.prototype,
propertyName: 'constructor'
};
function foo() {
alert(this.bar);
}
var x = {bar: 10};
var y = {bar: 20};
x.test = foo;
y.test = foo;
x.test(); // 10
y.test(); // 20
(function () {
alert(this); // null => global
})();
var foo = {
bar: function () {
alert(this);
}
};
foo.bar(); // Reference, OK => foo
(foo.bar)(); // Reference, OK => foo
(foo.bar = foo.bar)(); // global?
(false || foo.bar)(); // global?
(foo.bar, foo.bar)(); // global?
function foo() {
function bar() {
alert(this); // global
}
bar(); // the same as AO.bar()
}
var x = 10;
with ({
foo: function () {
alert(this.x);
},
x: 20
}) {
foo(); // 20
}
// because
var fooReference = {
base: __withObject,
propertyName: 'foo'
};
try {
throw function () {
alert(this);
};
} catch (e) {
e(); // ES3标准里是__catchObject, ES5标准里是global
}
// on idea
var eReference = {
base: __catchObject,
propertyName: 'e'
};
// ES5新标准里已经fix了这个bug,
// 所以this就是全局对象了
var eReference = {
base: global,
propertyName: 'e'
};
(function foo(bar) {
alert(this);
!bar && foo(1); // "should" be special object, but always (correct) global
})(); // global
function A() {
alert(this); // "a"对象下创建一个新属性
this.x = 10;
}
var a = new A();
alert(a.x); // 10
var b = 10;
function a(c) {
alert(this.b);
alert(c);
}
a(20); // this === global, this.b == 10, c == 20
a.call({b: 20}, 30); // this === {b: 20}, this.b == 20, c == 30
a.apply({b: 30}, [40]) // this === {b: 30}, this.b == 30, c == 40