Friday, September 11, 2015

bind, call, apply self-testing

name = "super-man";
var obj = {
  name :"mart-man",

  // general  hello : function(){
    console.log("hello  : " + this.name);
      this.test(function(){
        console.log("hello > callback : " + this.name);
      });
  },

  // bind  hello_bind : function(){
    console.log("hello_bind : " + this.name);
    this.test(function(one, two){
      console.log("hello_bind > callback : " + this.name);
    }.bind(this));
  },

  test : function(callback){
    callback();
  }
}

test = function(){
  console.log("out test");
}

console.log(1, obj.name);
console.log(2, name);
console.log(3);
obj.hello();
console.log(3.5);
var hello_outer = obj.hello;
hello_outer();
console.log(4);
obj.hello_bind();
console.log(5);
obj.hello.call(this);
obj.hello.apply({name:'kings-man', test:function(){console.log("third test")}});
console.log('end');


If you don't understand well why this logs printed.
I suguess this website and http://javascriptissexy.com/javascript-apply-call-and-bind-methods-are-essential-for-javascript-professionals/

study it~!


1 "mart-man"
2 "super-man"
3
hello  : mart-man
hello > callback : super-man
3.5
hello  : super-man
out test
4
hello_bind : mart-man
hello_bind > callback : mart-man
5
hello  : super-man
out test
hello  : kings-man
third test
end




No comments:

Post a Comment