What is the best way to declare functions in a Javascript class?

I am trying to understand the difference between two different function declarations in JavaScript.

Consider the following code snippet:

function SomeFunction(){

   this.func1 = function(){
   }

   function func2(){
   }
}

What is the difference between func1 and func2 declarations above?

+4
source share
5 answers

In simple language, it fun1is a property of a class SomeFunctioncontaining a reference to an anonymous function, where it func2is called a function.

Property

Here fun1is a property of this class SomeFunction, this means that when you create an instance of the SomeFunction class using the new keyword, you can access it only from the outside.

Private method

fun2 SomeFunction .


function SomeFunction() {
  this.func1 = function() { console.log("in func1") }
  function func2() { console.log("in func2") }
}

var obj = new SomeFunction();

obj.func1();  //Accessible 
obj.func2(); //Not accessible
+4
function SomeFunction() {

  // Function Expression
  this.func1 = function() {

  }
  // Function Declaration
  function func2() {

  }
}

Update:

: Javascript?

.

Person :

(2) :

(1) :

  • showPerson1()

function Person(name, country) {
  // Attributes of Person class.
  this.name = name;
  this.country = country;

  // Function Expression:
  this.showPerson1 = function() {
    var personMessage = "Hi, my name is ";
    personMessage += this.name;
    personMessage += ". I'm from ";
    personMessage += this.country;
    personMessage += ".";
    return personMessage;
  };

  // Function Declaration:
  function showPerson2() {
    var personMessage = "Hi, my name is ";
    personMessage += this.name;
    personMessage += ". I'm from ";
    personMessage += this.country;
    personMessage += ".";
    return personMessage;
  }
}

var person1 = new Person("John", "United States");
console.log(person1.showPerson1()); // Prints: Hi, my name is John. I'm from United States.
console.log(person1.showPerson2()); // Throws an error.

, showPerson1(), Person, this .

showPerson2(), Person.

.

+2

. . , .

var func1 = function() {
     // Code here
}

. . . Hoisting Javascript

func1 () {
     // Code here
}

. , - . Javascript, - .

, .

, .

function SomeFunction(){
    
  this.func1 = function() {
    console.log('Public function.')
  }
  
  function func2() {
    console.log('Private function.')
  }
  
  this.func3 = function() {
    func2();
  }
  
}

var obj = new SomeFunction();

obj.func1(); // Public Function.
// Can be accessed as it is a Public function
obj.func3(); // Private Function.
// Can be used to access a private function.
obj.func2(); // Error: Uncaught TypeError
// Private function

, !:)

+1
source

func1actually a property on SomeFunctionso you can do

const someClass = new SomeFunction();
someClass.func1()

But you cannot do

const someClass = new SomeFunction();
someClass.func2()

because he is not attached to this class. However, you can still use func2inside your class SomeFunction, but not outside the class.

ES6 provides cleaner syntax to figure this out.

class SomeClass{
  func1(){
    //function body
  }
}

Hope this helps

0
source

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


All Articles