Trying to understand scope in two short JavaScript functions

What is the difference between the following two JavaScript functions? I know that variables declared with var are local inside the function, and if declared with the keyword this`, they are exposed to an external word. is there any other difference between

 function student(param1, param2, param3) { this.name = param1; this.age = param2; this.address = param3; } 

and

 function student(param1, param2, param3) { var name = param1; var age = param2; var address = param3; } 
+5
source share
4 answers

Short answer: you would use the first for the constructor. The second function does nothing. If you want to use "private variables", access the functional cloud variables in the counter constructor using instance methods through closure.

This function will be used as follows to create a student. The transferred parameters are assigned to the newly created student object.

  function student(param1, param2, param3){ this.name = param1; this.age = param2; this.address = param3; } var student1 = new student('steve', 22,'333 E 3rd Ave'); var student2 = new student('rachel', 34,'111 N 1st St'); console.log(student1.name); // 'steve' console.log(student2.name); // 'rachel' 

The second will not satisfy for the constructor.

It declares variables with a functional area that remain unused. As soon as the student function completes, the 3 variables defined internally will be garbage collected. This function does nothing.

What do you expect from this feature? It cannot be used the same way:

  function student(param1, param2, param3){ var name = param1; var age = param2; var address = param3; } var badStudent = new student('Greg', 22,'222 W 2nd Rd'); console.log(badStudent.name); // undefined 

Edit

Someone explained how to make "private member variables" using variables declared with var inside the constructor. Use shutters:

 function student(param1, param2, param3) { var name = param1; var age = param2; var address = param3; this.getName = function(newName) { if (newName) name = newName; return name; }; } var myStudent = new student('steve', 22,'333 E 3rd Ave'); console.log(myStudent.name); console.log(myStudent.getName()); console.log(myStudent.getName('dan')); console.log(myStudent.getName()); 

Note that since the getName instance getName refers to the scope variable declared in the constructor, there remains a closure that refers to these variables. Due to closure, there are still references to variables after the constructor completes, and they are not garbage collected. Thus, using the instance method, you can get and set this variable, which cannot be accessed through the resulting constructor object.

+3
source

The main difference between this. variables this. vs var is the area. Variables declared as part of this will be part of objects, and variables declared with var can be private. Might because it depends on your return . If you do not use return , then they will be closed

Example

 function Student(fname, lname, dob) { var _fname = fname, _lname = lname, _dob = new Date(dob); this.fullName = _fname + " " + _lname; this.age = (new Date()).getFullYear() - _dob.getFullYear(); } var stu = new Student('foo', 'bar', '1998/11/13'); console.log(stu); console.log(stu._fname) 

As you can see, _fname is passed and stored using var . Thus, its volume does not work yet. Therefore, when you try to access it outside of the function, it is not available.

So you can use this to define public properties and use var to define private ones.

+1
source

In JavaScript, to create an object, we use 'function' as a constructor; this constructor of a function basically returns an object. when you declare a variable with "var" moments "this.var name", in this case you are trying to use this variable to create an object. This variable declared with "var" is a local variable inside the function.

on the other hand, when you use "this.variableName", you create a property for the object that the constructor function is trying to create.

'this is'. refer to the object that the constructor function creates. "var variableName" is only a local variable and is not a property of the 'this' object.

 function student(param1,param2,param3){ this.name=param1; this.age=param2; this.address=param3; } var t=new student('farhad',28,'address'); 

will create this object:

 t{ name:'farhad', age:28, address:'address' } 

and

 function student2(param1,param2,param3){ var name=param1; var age=param2; var address=param3; } var t2=new student2('farhad',28,'address'); 

will create this object:

 t2{ } 

in 't2' you don't see any property

+1
source

this used inside the function and contains the value of the object calling the function

Here, this refers to an instance of an object and is not assigned a value until the object calls the function in which it is defined

 function Student(param1, param2, param3) { this.name = param1; this.age = param2; this.address = param3; this.print = function() { console.log(this.name + this.age + this.address) } } var stud = new Student('a', 'b', 'c'); stud.print(); // abc 

In a later case, var name=param1;var age=param2;var address=param3; you assign each of the parameters to a variable, and these variables have a scope only inside the function

0
source

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


All Articles