function Person(name,age){
this.name = name;
this.age = age;
}
var person1 = new Person("张三",18);
var person2 = Person("李四",12);
console.log(person1);
console.log(person2);
console.log(person1.name, person1.age);
console.log(window.name, window.age);
function test(arg) {
this.x = arg;
return this;
}
var x = test(5);
var y = test(6);
console.log(x.x);
console.log(y.x);
var x = test(5); => window.x = window.test(5)
window.x = 5; x = window; => window.x == x == window
var y = test(6); => window.y = window.test(6)
window.x = 6; y = window; => window.x == x == 6
x.x => 6.x => undefined
y.x => window.x => 6
var obj = {
data: [1, 2, 3, 4, 5],
data2: [1, 2, 3, 4, 5],
fn: function () {
console.log("--test--");
console.log(this);
return this.data.map(function (item) {
console.log(this);
return item * 2;
});
},
fn2: function () {
console.log("---test2---");
console.log(this);
return this.data2.map((item) => {
console.log(this);
return item * 2;
});
},
};
obj.fn();
obj.fn2();