"I learned the basics of programming in Ruby, so let's try js next time!" ↓ "I don't know what it is because it's messed up!"
Is there such a person?
I myself used to stuff it too much and it became a bun, so I tried to summarize the differences in writing style in an easy-to-understand manner. You don't have to remember each one, so please stock this article. I think you will deepen your understanding of object-oriented programming, so please take a look to the end!
Output of basic characters. It's difficult because the processing is different depending on the language, but it's basic, so let's hold it firmly. After all Java has a long description w
python
  #Ruby
  puts "Hello"
python
  #PHP
  echo "Hello";
  //Java
  System.out.println("Hello");
  //JavaScript
  console.log("Hello");
It is a super basic of programming, variable assignment.
python
  #Ruby
  name = "Tom"
python
  #PHP
  $name = "Tom";
  //Java(Specify a type such as String, int, etc.)
  String name = "Tom";
  //JavaScript
  let name = "Tom";
I also use this a lot. They are all similar, but there are differences. * The writing method is just an example.
python
  #Ruby
  "my name is#{name}is."
python
  #PHP
  "my name is{$name}is."
  //Java
  "my name is"+ name + "is."
  //JavaScript(''not``(Back quotation)Surround with)
  `my name is${name}is.`
The representative of processing is the if statement. Note the difference in elsif.
python
  #Ruby
  if age >= 20
    #processing
  elsif age >= 10
    #processing
  else
    #processing
  end
python
  #PHP
  if (age>= 20){
    #processing
  }elseif (age >= 10){
    #processing
  }else{
    #processing
  }
  //Java & JavaScript
  if (age>= 20){
    //processing
  }else if (age >= 10){
    //processing
  }else{
    //processing
  }
I often use array retrieval, and there are various methods for extracting one by one depending on the language.
python
  #Ruby
  names = ["Tom","Kenta","John"]
  names[0]
python
  #PHP
  $names = array("Tom","Kenta","John");
  $names[0];
  //Java
  String names[] = {"Tom","Kenta","John"};
  names[0];
  //JavaScript
  const names = ["Tom","Kenta","John"];
  names[0];
Be careful when searching, as the names differ depending on the language.
python
  #Ruby(hash)
  user = {name: "Tom",age: 20}
  user[:name]
python
  #PHP(Associative array)
  $user = array("name" => "Tom","age" => 20)
  $user["name"]
  //JavaScript(object)
  const user = {name: "Tom",age: 20};
  user.name
This is the important point. This time I created an add method (function) that sums the two values. The return value is also important, so hold it firmly.
python
  #Ruby
  def add(a, b)
    return a + b
  end
  sum = add(5, 2)
python
  #PHP
  function add($a, $b){
    return $a + $b;
  }
  $sum = add(5, 2);
  //Java
  public static int add(int a, int b){
  //int after static specifies the return type, use void for methods with no return value
    return a + b; 
  }
  int sum = add(5, 2);
  //JavaScript
  const add = function(a, b){
    return a + b;
  };
  let sum = add(5,2);
  //Or
  const add = (a, b) => {
    return a + b;
  };
  let sum = add(5,2);
  //Or
  function add (a, b){
    return a + b;
  }
  let sum = add(5,2);
The recommended description of js differs depending on the version.
The long-awaited object-oriented beginning! A Menu class is created, and an instance is created based on that class and assigned to the variable menu1.
python
  #Ruby
  class Menu
    ##processing
  end
  menu1 = Menu.new
python
  #PHP
  class Menu{
    ##processing
  }
  $menu1 = new Menu();
  //Java
  class Menu{
    //processing
  }
  Menu menu1 = new Menu();
  //JavaScript
  class Menu{
    //processing
  }
  const menu1 = new Menu();
The menu contains "information" such as name and price. Declare it in advance in the class.
python
  #Ruby
  attr_accessor :name
  attr_accessor :price
python
  #PHP
  private $name;
  private $price;
  //Java
  private String name;
  private int price;
  //JavaScript
  //No predefinition required?
Now, I've come to a place I don't understand! The initial method is the method that is called when it is new (the very first). This time, the value specified in the new argument is entered in the variable as instance information in the initial method.
python
  #Ruby
  def initialize(name, price)
    self.name = name
    self.price = price
  end
  menu1 = Menu.new("Hamburger",300)
python
  #PHP
  public function __construct($name,$price){
    this->name = $name;
    this->price = $price;
  }
  $menu1 = new Menu("Hamburger",300);
  //Java
  //Define a method with the same name as the class in the class
  Menu(String name, int price){
    this.name = name;
    this.price = price;
  }
  Menu menu1 = new Menu("Hamburger",300)
  //JavaScript
  constructor (name, price){
    this.name = name;
    this.price = price;
  }
  const menu1 = new Menu("Hamburger",300)
Well last. Instances have "processing" in addition to "information". This time, how to define and call the process. Since the instance created from the Menu class is assigned to the variable menu1, The output will be "This hamburger costs 300 yen".
python
  #Ruby
  def show
    puts "this#{self.name}Is#{self.price}It's a yen"
  end
  menu1.show
python
  #PHP
  public function show(){
    echo "this{$this->name}Is{$this->price}It's a yen";
  }
  $menu1->show();
  //Java
  public void show(){
    System.out.println("this"+this.name+"Is"+this.price+"It's a yen");
  }
  menu1.show();
  //JavaScript
  show(){
    console.log(`this${this.name}Is${this.price}It's a yen`);
  }
  menu1.show();
Thank you for watching until the end. There may be omissions and mistakes, but please be patient! !!
It is said that it is not so good to learn shallowly and widely in this world, but I think it is good to try another language to deepen your knowledge.
Recommended Posts