How can you use the 'this' keyword inside a function signature to set a default parameter?

I have a function bound to a React component:

    functionName(param1, param2 = this.state.field) {

    }

I have never heard of the 'this' keyword, which is used in a function signature in Javascript, and I wonder if the 'this' context will be a storage object or a global scope. Should this function be bound to the proper context before I can properly use 'this' inside the signature?

+4
source share
2 answers

I am wondering if the 'this' context will be a storage object or a global scope.

. , . this , - . , , , this .

+2

, React.Component . "this" . :

import React from 'react';
class example extends React.Component {
    this.state = {
        field: "this variable!"
    }
    functionName(param1, param2 = this.state.field) {
        console.log(param2);
    }   
}

, this.state example, , .state.

, . , JavaScript - , , .

+1

Source: https://habr.com/ru/post/1681207/


All Articles