Let's write Python, Ruby, PHP, Java, JavaScript side respectively

at first

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.

JavaScript code: squirrel:

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);

Ruby code: gem:

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()

Python code: snake:

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()

PHP code: elephant:

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();

Java code: coffee:

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");
  }
}

Finally

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

Let's write Python, Ruby, PHP, Java, JavaScript side respectively
Java VS PHP VS Python VS Ruby
Summary of how to write increment / decrement (Scala, Java, Rust, C, C ++, Go, PHP, Perl, Python, Ruby, JavaScript)
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
How to handle JSON in Ruby, Python, JavaScript, PHP
Let's write python with cinema4d.
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
[Swift / Ruby / Python / Java] Object-oriented programming
Dynamic proxy with python, ruby, PHP
[Python] Let's write briefly about comprehensions
Behavior of division operators between integers (C, C ++, Scala, Java, Rust, Go, PHP, JavaScript, Perl, Python, Ruby)
Ruby Python Java Case insensitive sorting
Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
String literals in each language: single quotes, double quotes, interpolation, escape sequences (Java, JavaScript, Kotlin, PHP, Python, Ruby)
Let's throw away JavaScript and write a web front end in Python!
Realize PHP / Python generator with Golang / Ruby
Closure 4 language comparison (Python, JavaScript, Java, C ++)
(Java, JavaScript, Python) Comparison of string processing
Express Python yield in JavaScript or Java
[Basic grammar] Differences between Ruby / Python / PHP
How to write Ruby to_s in Python
Let's write FizzBuzz with an error: Python Version
Let's write a Python program and run it
paiza POH ec-campaign (C # / Java / Python / Ruby) # paizahack_01
Grouping combination in Python / Ruby / PHP / Golang (Go)
Let's see how to count the number of elements in an array in some languages [Go, JavaScript, PHP, Python, Ruby, Swift]