L
L
Los Angeles2015-09-29 21:50:02
JavaScript
Los Angeles, 2015-09-29 21:50:02

What does the this keyword refer to?

In general, there is such an example:

'use struct';

Function.prototype.bind = function(o){
  var self = this,
      boundArgs = arguments;

  return function() {
    var args = [], i;

    for (i = 1; i < boundArgs.length; i++) args.push(boundArgs[i]);
    for (i = 0; i < arguments.length; i++) args.push(arguments[i]);
    return self.apply(o, args);
  };
};

function hello(){
  return console.log(this.name);
}

var a = {
  name: 'Alex'
};

var x = hello.bind(a);

x()

The return function from the outer function returns self.apply(o, args) ;
Question: What does self refer to ? (well, or what this external function refers to).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2015-09-29
@cleverpope

In this call, hello.bind, hello is an Object Function.
It refers to it.

I
Ivan Solomennikov, 2015-09-29
@ivsol

If the function is called as a method, then the this keyword will point to the current object, if as a function, then this will point to the global object in non-strict mode and in strict to undefined.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question