JavaScript - understand methods

I have doubts about understanding the principles of methods. I understand the functions, and I know that

function hello() {
    alert('Hello World');
}  

coincides with

var hello = function() {
    alert('Hello World');
}  

Now what is my problem.
Here is my object with one method. I don’t understand why brackets are not needed yarsLeftinside. function people()
I'm looking for a logical explanation.

function people(name, age) {
    this.name = name;
    this.age = age;
    this.yearsUntilRetire = yearsLeft; // this is confised for me
}

function yearsLeft() {
    var numYears = 65 - this.age;
    return numYears;
}

Create Object

var test = new people('Superman', 50);
test.yearsUntilRetire(); // I understand this code and calling a method in that way

Why can't I write this.yearsUntilRetire() = yearsLeftorthis.yearsUntilRetire = yearsLeft();

+4
source share
3 answers

When you use a function name without parentheses ( ()), you set a link to the function itself.

When you use the same function with parentheses, you perform that function.

Therefore this line

this.yearsUntilRetire = yearsLeft;

yearsUntilRetire, . :

this.yearsUntilRetire(); // execute the yearsLeft function.

this.yearsUntilRetire() = yearsLeft this.yearsUntilRetire = yearsLeft();

- .

- yearsUntilRetire yearsLeft

+7

1) function hello() {alert('Hello World');} var hello = function() {alert('Hello World');}

- . - , hello. , , , , .

2) , , : yearsLeft(). .

- JS. , , , .

yearsLeft property yearsUntilRetire. , , - " - "

+1

, . . .

console.assert( typeof helloWorldAsVar === 'function', 'helloWorldAsVar is defined' );
console.log( helloWorldAsVar );
console.assert( typeof helloWorld === 'function', 'helloWorld is defined' );
// creating a function like helloWorld means it will be hoisted to the top of the file as js does two passes
// when compiling the script. one to declare all the functions and initialize all the variables as undefined.
// and another to run through the code.
console.log(helloWorld);


var helloWorldAsVar = function() {
  return 'this will not get hoisted, and the function name is anonymous';
}
// helloWorldAsVar is only available after it is declared.
console.assert(typeof helloWorldAsVar === 'function', 'helloWorldAsVar is defined');
console.log( helloWorldAsVar );

function helloWorld() {
  return 'this will get hoisted to the top of the function, and it will keep its name';
}
<script src="http://codepen.io/synthet1c/pen/WrQapG.js">
</script>
Hide result

// this is a constructor function, it is essentially a typed object that can
// have it own methods similar to an oop class in other languages like php or java
// the convention is to name a constructor with the first letter capitalized and it should be singular
function Person(name, age) {
  // here you are assigning properties to your People object
  this.name = name;
  this.age = age;
  // you are passing a reference to the yearsLeft function
  // this.yearsUntilRetire = yearsLeft;
}

// a more standard way to add the yearsLeft function is to 
// use the People.prototype object which get methods delegated to it eg.
Person.prototype.yearsUntilRetire = yearsLeft;

function yearsLeft() {
  var numYears = 65 - this.age;
  return numYears;
}

// Create objects
var superman = new Person('Clark', 50 ),
    batman = new Person('Bruice', 40 );

console.log( superman, 'yearsUntilRetire', superman.yearsUntilRetire() );
console.log( batman, 'yearsUntilRetire', batman.yearsUntilRetire() );

// Why I can 't write this.yearsUntilRetire() = yearsLeft or this.yearsUntilRetire = yearsLeft();
console.assert(this === window, 'this in the global context refers to the window object' );
console.assert(superman instanceof Person, 'superman is a Person' );
console.assert(batman instanceof Person, 'batman is a Person' );
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
Hide result
+1

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


All Articles