0

I'm studying the following code, but having trouble understand the keyword this.

Is the "this" in e.stunHost && this.init() the same as init: function() { this.answer = "" }

Thanks.

The following code is stripped from a module about RrcPeerConnection.

define("WebRtcPeer", ["jquery", "Logger"], function(e, t) {
        "use strict";
            var i = function(e) {
            e.stunHost && this.init()
            };
        return i.prototype = {
            stunHost: null,
            init: function() {
                this.answer = ""
            }
    });
Chiu
  • 350
  • 4
  • 14

2 Answers2

0

Yes, they are same, all point to function i.

SantinoDu
  • 43
  • 4
  • I tried _var i = function () { return this };_ in jsfiddle, but it return the _Window object_. Are they different? – Chiu Aug 19 '16 at 03:14
  • check nmrony's link, you will understand why here this point to Window, it's not use keyword "new", find prototype chain to window. – SantinoDu Aug 19 '16 at 08:14
0

this is a special keyword that refers to the current object. To learn about this keyword in javascript check the link this and Object prototype

Nur Rony
  • 7,823
  • 7
  • 38
  • 45