var value = 10
var obj = {
value:100,
method:function(){
var foo = function(){
console.log(this.value)
console.log(this)
}
foo()
return this.value
}
}
obj.method()
-
var number = 10;
function Person(){
number = 20
this.number = 30
}
Person.prototype.getNumber = function(){
return this.number;
}
var p = new Person()
console.log(p.getNumber())
-
var value = 10;
var obj = {
value:20
}
var method = function(){
console.log(this.value)
}
method()
method.call(obj)
method.apply(obj)/20
var newMethod = method.bind(obj)
newMethod()
-
var value = 10
var obj = {
value:100,
method1:function(){
console.log(this.value)
var foo = function(){
console.log(this.value)
}
}
}
console.log(obj.method())
-
function fn(k){
this.m = k
return this
}
var m = fn(1)
var n = fn(2)
console.log(m.m)
console.log(n.n)
-
var myObject = {
foo: "bar",
func: function() {
var self = this;
console.log(this.foo);
console.log(self.foo);
(function() {
console.log(this.foo);
console.log(self.foo);
}());
}
};
myObject.func();
-
var color = 'green';
var test4399 = {
color: 'blue',
getColor: function(){
var color = "red";
console.log(this.color);
}
}
var getColor = test4399.getColor;
getColor();
test4399.getColor();