Loss of the challenge context



  • Please explain why after the attribution. var f = obj1.f loses the context of the challenge and emerges undefined?

    var obj1 = {
    

    x: 3,

    f: function() {
    return (this.x);
    }
    };

    alert(obj1.f());
    var f = obj1.f;
    alert(f());



  • Importance this within the function, as a result function and function as established function.

    How are you?

    The following can be called for:

    Purpose of the function

    If there is a normal function, in most cases the value this There will be a global facility. window) Use "strict mode."undefined

    var f = function (){
      console.log('common:',this.toString());
    };
    f();
    

    var fStrict = function (){
    "use strict";
    console.log('strict:', this);
    };
    fStrict();

    Usually, this is the function ofalignment.callbackthis is why this They seem unexpected.

    For example, when a function is transferred https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener value this There will be an element added by the processor.


    Method issue

    The method is the function of the facility.

    var Obj = {toString:function(){ return "[object Obj]";}};
    Obj.f = function (){
    console.log('common:',this.toString());
    };
    Obj.f();

    Obj.fStrict = function (){
    "use strict";
    console.log('strict:', this.toString());
    };
    Obj.fStrict();

    When the function is called the method, the value this is the object in which the function is located, in fact, the value before the symbol of the point.


    Designer Challenge

    The function can be called as a designer, so the operator needs to be used before the call. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/new : new Foo()

    function Foo(name){
    this.name = name;
    }

    var foo = new Foo('foo');
    console.log(foo);

    When the function is called as a designer, a new facility shall be created and the value shall be added this refers to this facility.

    Specificity: in the use of inheritance and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes appeal this to call https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super Depending on the browser, an exception to the attempt to turn to an undeclared/non-initialized variable.

    class A {}
    class B extends A {
    constructor(){
    console.log(this);
    }
    }
    var b = new B();


    Challenge through methods https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/call and https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

    Use of functions https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/call and https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/apply The importance can be given. this directly, transfer it first.

    var f = function (){
    console.log('common:',this);
    };
    f.call({o:'object'});

    var fStrict = function (){
    "use strict";
    console.log('strict:', this);
    };
    fStrict.apply({o:'object'});

    In libraries like https://jquery.com/ Through these functions, there are bonds transferred to various functions, such as: http://api.jquery.com/each/ ♪ http://api.jquery.com/map/ ♪ http://api.jquery.com/map/ and others. As this In this case, the current element of the collection or the html element shall be established.


    Challenges as collbecks in mass processing functions

    Some built-in functions for a type object Array allow for the same direct reference this for transferable collback:

    • https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/every
    • https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
    • https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/find
    • https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
    • https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
    • https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/map
    • https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/some

    var specialMap = {'b':'specialB','d':'specialD'}
    var source= ['a','b','c','d','e'];
    var mapped = source.map(function(el){
    return this[el] || ('common-'+el);
    },specialMap);

    console.log('source:',source);
    console.log('mapped:',mapped);

    How's it going?

    Announcement of function or functional expression

    Normal declaration of function:

    function A(){}

    var a = function (){};

    at normal declaration of value this shall be determined when summon described above.


    Creation of a function https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

    Function https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/bind returns new attached function. Importance thiswithin the established function Always. The one that was handed over at the challenge. https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/bind ♪

    Important feature: using the attached function as a designer, value this will still indicate the facility being created as described above.

    Important feature: Total NOT strict mode as a parameter this values null and undefined - this parameter will be ignored and this will be installed in a global facility.

    function A(){console.log(typeof this,'is window', this === window);}
    console.log('execute with null');
    A.bind(null)();
    console.log('execute with undefined');
    A.bind(undefined)();

    function A1(){'use strict'; console.log(typeof this, this);}
    console.log('execute with null');
    A1.bind(null)();
    console.log('execute with undefined');
    A1.bind(undefined)();

    Important feature: value this established function No. redesign using functions https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/call and https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Function/apply as described above.

    function A(){console.log(this);}

    var B = A.bind({o:'object'});
    console.log('execute binded');
    B();
    console.log('execute with call');
    B.call({another: 'some new object'});

    console.log('execute as constructor');
    new B();


    https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    Structural responsibilities have emerged in ES2015 and are tied to the current value this

    After the importance this You can't change the above-mentioned ways.

    In addition, the shooter function No. Use as a designer.

    function A(){
    this.t = (place)=>console.log(place,this);
    }

    var a = new A()
    a.t('method:');

    var tt = a.t;
    tt('free function execute:');

    tt.call({o:'object'},'using call function');

    new tt('constructor');


    Based on replies:
    ♪ https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work
    ♪ https://stackoverflow.com/questions/133973/how-does-this-keyword-work-within-a-javascript-object-literal




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2