Let’s start with a question:
Why is JavaScript called “JavaScript”?
The answer to this question goes back to the history of JavaScript.
In May 1995, Netscape created a browser-side scripting language called Mocha.
Soon after, however, the name was changed to LiveScript in September of that year.
Not long after that, in December of that year, Netscape teamed up with Sun and renamed it “JavaScript,” which stands for Script like Java.
Why did you settle on the name “JavaScript”? How similar are “JavaScript” and “Java”?
Let’s do a quick comparison with a few pieces of code. Since it’s “like Java”, let’s use Java for comparison. And Java as a pure object-oriented language (everything is object class), we will start from Java object-oriented three characteristics for comparison.
encapsulation
Encapsulation, as the primary feature, refers to the ability to hide or expose properties or functions of an object.
We create an Animal object that has a private property name and can only be changed/obtained by the set/get method on the object. The specific code is as follows:
// Java class Animal { private String name; public void set(String name) { this.name = name; } public String get() { return this.name; } } public class Test { public static void main(String[] args) { Animal animal = new Animal(); animal.set("dog"); System.out.println(animal.get()); // "dog" } } // JavaScript(ECMAScript5) function Animal() { var name = '' this.set = function(a) { name = a } this.get = function() { return name } } var animal = new Animal() animal.set('dog') console.log(animal.get()) // 'dog'
Copy the code
For the “encapsulation” feature, both can be implemented, but there are some differences.
-
A class in Java is an object that can be encapsulated using the keywords private and public.
-
The scope in JavaScript is a function, so you need a function to implement it.
inheritance
Inheritance features in Java allow subclasses to obtain properties and methods defined in their parent class.
class Dog extends Animal {
public String bark() {
return "wang wang wang!!!";
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.set("dog");
System.out.println(dog.get()); // "dog"
System.out.println(dog.bark()); // "wang wang wang!!!"
}
}
Copy the code
The JavaScript implementation inherits the Prototype attribute in the function, pointing it to an instance of the parent class.
function Dog() { this.bark = function() { return 'wang wang wang!!! ' } } Dog.prototype = new Animal() var dog = new Dog() dog.set('dog') dog.get() // 'dog' dog.bark() // 'wang wang wang!! ! '
Copy the code
There are some differences between JavaScript and Java in inheritance: you need to make the prototype attribute point to the parent class instance after the function declaration to achieve the effect of inheritance, and you need to merge multiple instances of the parent class. The maintainability and readability aspects of code are obviously not defined by Java as in class declaration.
polymorphism
Taking functions as an example, function overloading is commonly used when a function needs to perform different operations on different input results. In the following example, we write a Bark method that displays for different inputs.
Class Cat {public String bark(String sound) {return sound; } public String bark() {return "..." ; } } public class Test { public static void main(String[] args) { Cat cat = new Cat(); System.out.println(cat.bark()); / / "..." System.out.println(cat.bark("miao~")); // "miao~" } }
Copy the code
JavaScript, on the other hand, is more “sad”. The language itself does not support overloaded functions. If a function is defined multiple times, it will be overwritten, so the only way to achieve polymorphism is by judging the input parameter.
function Cat() { this.bark = function(sound) { if(undefined ! == sound && null ! == sound) { return sound } else { return '... ' } } } var cat = new Cat() console.log(cat.bark()) //'... ' console.logcat.bark('miao')) // 'miao'
Copy the code
The difference between JavaScript and Java is further amplified in the case of polymorphism, because JavaScript has no variable type declarations and functions do not support overloading.
More and more
The list of language differences between JavaScript and Java could go on, but one of the most notorious is that JavaScript is a weakly typed language, which makes code less robust: a function can pass in any value that might cause an error, but it can only be discovered at execution time. Of course, more advanced interface functionality is also not supported by JavaScript native.
As you can see from the code analysis above, there are still some significant differences between JavaScript and Java, and the good news is that the ES6 version has made significant improvements to JavaScript, making it more efficient and robust. Even more impressive is the Typescript glue language, which makes code more like Java Script.
Let’s rewrite the above three classes in Typescript:
// Encapsulate class Animal {private name:string; Public set(name:string):void {this.name = name} public get():string {return this.name}} // Extends class Dog Animal { public bark():string { return 'wang wang wang!!! Public call():string public call(sound:string):string public call(sound:string):string public call(sound:string) :string):string { if(undefined ! == sound && null ! == sound) { return sound } else { return '... '}}}
Copy the code
Compared to Java, Typescript is very similar in terms of encapsulation and inheritance, with minor differences such as case and before and after type declarations. In terms of polymorphism, Typescript adds type declarations, but the JavaScript language limits it to a single function implementation, which still requires argument judgment.
conclusion
If ES6 makes JavaScript more like Python, Typescript as a superset of ES6 makes JavaScript more like Java for what it deserves. On the one hand, it allows developers to write robust and efficient code, and on the other hand, it makes it more likely that JavaSript will become a back-end language running on Node.js. This is important for front-end developers as well as for JavaScript full-stack developers.
This article may be forwarded or shared, but must retain the complete picture and text information and source, the author reserves the right to investigate all legal responsibility and means ~
Welcome to search the concerned public number “Web learning society” ~