I wrote the code for Python, Ruby, PHP, Java, and JavaScript when I published the performance comparison report for each code in class, so I will omit them and list only the side: smiling_imp: I would be very grateful if you could tell me how to write it incorrectly, or if there is something that should be done individually.
This time, as a sample, a class is created, a fuga method is defined, and fuga is called as an initial process.
Since there is no class, make it a closure, create a prototype, and call the fuga method from the constructor.
.js
var Hoge = (function(value) {
  //Initial processing
  function Hoge(value) {
    this.hoge = value;
    this.fuga();
  }
  //fuga method
  Hoge.prototype.fuga = function() {
    console.log("fuga");
  };
  return Hoge;
})();
new Hoge(10000000);
Create a class and call the fuga method from ʻinitialize` as the initial process
class Hoge
  HOGE = "hoge".freeze
  #Initial processing
  def initialize()
    fuga
  end
  #fuga method
  def fuga
    puts "fuga"
  end
end
Hoge.new()
Create a class and call the fuga method from __init__ as the initial process
class Hoge(object):
    HOGE = "hoge"
    #Initial processing
    def __init__(self):
        self.fuga()
    #fuga method
    def fuga(self):
        print "fuga"
Hoge()
Create a class and call the fuga method from __construct as the initial process
.php
<?php
class Hoge
{
    #Initial processing
    public function __construct()
    {
      $this->fuga();
    }
    #fuga method
    private function fuga()
    {
      print "fuga";
    }
}
new Hoge();
Call the fuga method from the main method
public class Hoge {
  private staitc final String HOGE = "hoge";
  //Initial processing
  public static void main(String[] args) {
    new Hoge().fuga();
  }
  
  //fuga method
  public void fuga() {
    System.out.println("fuga");
  }
}
I compared the four codes.
When I wrote class and method and compared them, there was not much difference, but there are some differences for each language.
Recommended Posts