One, foreword
As the future heir to the Java empire, Prince Java received a strict education, not only proficient in the Java language, Java virtual machine, Java class libraries and frameworks, but also a variety of official Java specifications.
Recently, he heard that a guy called Javascript had made a comeback and succeeded in building an independent empire that was not only the king of front-end programming, but also encroached on the Java empire!
According to the little prince’s court teacher: in those days, this guy just ran in the browser, completely rubbed off the heat of Java this development, now unexpectedly turned to bully us, there is no reason? What is intolerable? !
The little prince does not think so, existence must be reasonable, javascrip must have unique, as the saying goes, know yourself, know your enemy, a hundred battles are not dangerous, he thinks it is necessary to explore the kingdom of Javascript, collect information, to see what is going on in this browser object-oriented language, why so many code farmers flock to it.
Second, a preliminary impression
After dressing up in disguise, the little prince comes to Javascript kingdom, which seems to be a lively scene, people are spontaneous and free, unlike Java empire so serious and rigid, let a person feel happy.
But to the little prince’s surprise, there was no official library! AngularJS, React, Backbone, Vue, Ember, JQuery,…… Each other also quarrel to quarrel, fight to fight, is very lively.
In contrast, the Java empire has a tight governance, with a huge class library provided by the government, and a dominant Web framework SSH/SSM, plus a variety of Java specifications, code farmers only need to learn, work on the line.
No choice of trouble, but also reduce the right to choose, is good or bad? The little prince himself did not know.
The prince also noticed that almost no one in the Javascript kingdom uses an IDE to write programs. You can just grab a handy text editor and start, and then throw it into a browser to run tests. What a lightweight thing! Alas, we in the Java empire are still arguing about IntelliJ IDEA versus Eclipse, which is unnecessary.
How to create an object without a class?
As the investigation deepened, the little prince felt more and more surprised that there was no such thing as class! An object-oriented language has no classes! This was the very opposite of what the little prince had been taught from birth.
How can you create an object without a class? When I was young, the court teacher often said: Write a class first, and then create an object from this class.
Instead, there are countless javascript objects coming and going, toiling together to support vast, vibrant empires.
Where do these objects come from? The little prince was puzzled. It was midday when he saw a JSON tavern in front of him and decided to rest his feet and have a good meal.
The little prince had ordered two pounds of cooked beef and three bowls of wine, and was just beginning to enjoy them, when a man in a long robe at a table next to him asked, “Well, what is the model of your object?
The king has just decreed that the archetype method is the secret of our empire, and it is forbidden to discuss it openly lest it be learned by the Java Empire.
The little prince was so moved that he immediately called his mistress to him and asked for the best wine and food to be served at a table next to him. After a few drinks, the little prince finally gains the initial trust of the two men, who are also responsible for reviewing the JavScript language specification.
“My family has been in business for generations. I have been to the kingdom of C++, the kingdom of Java and the kingdom of C#. They are all object-oriented languages with a class and an object distinction.
The bespectacled officer said, “We don’t use class, it’s too unintuitive!”
When I first learned Java, I had a hard time accepting the concept of class. In fact, an object-oriented system is an interaction between objects. What do we need classes for?
Then the little prince asked the crucial question: “How can you create an object without a class?”
“Stranger, not so complicated, you think about what is an object ah, isn’t it attributes plus methods? Let’s create an object, “the official said, writing on the table with his fingers dipped in the drink:
See, this animal object has an attribute name and a method eat. Simple, right?”
An object was created without the need for class, and a new door seemed to open for the little prince. “Since the object is not associated with a class, we can add attributes to the object at will,” the glasses official added.
“Can you play like that? !” The little prince was surprised. Without class constraints, these objects are too free.
How to inherit without class?
“How can inheritance be implemented? Inheritance is an important concept of object orientation,” the glasses official said. “It’s simple. In the javascript world, every object has a special property called Proto, which you can use to associate with another object (this object is called a prototype). Let me draw it for you.”
The code written by wine is not long, but it deeply shocked the little prince, because it is very large information, hidden the secret of the “prototype”, the little prince could not help thinking:
The object dog is modeled after animal, and the object Cat is modeled after animal.
Neither dog nor cat defines the eat() method, so how can it be called?
When the eat method is called, first look in the list of its own methods. If it can’t find it, look for the method in the prototype. If it can’t find it, look for…… in the prototype If you can’t find an Object, it’s undefined.
These objects here must have created a prototype chain through Proto!
Well, when my master told me about the JVM virtual machine, he also mentioned that when an object executes a method, it needs to find the definition of the method. The search sequence is also from the class to which the object belongs first, then the parent class, and then the parent class…… Up to Object, the idea is exactly the same!
It’s just that the Java method definition is in the class, and the javascript method is in the object, so now I think it’s a little bit more intuitive in the object.
Properties and methods should be similar, looking up the prototype chain, except that dog’s name attribute overrides animal’s name attribute, and this, when called dog.eat(), should refer to the dog object.
I guess the idea of object orientation is all figured out. The little prince smiled as he thought of it.
Seeing that the little prince had become stuck, the robed official gave him a push: Stranger, what’s the matter with you? “Oh, that’s all right,” said the little prince, realizing his gaffe. “I think you have a very clever way of using the prototype, of inheriting without classes at all.”
The glasses officer was stunned: “Stranger, it seems that you have a good understanding, you have been given insight into the secrets of the empire, but many new programmers are not easy to realize this, so we made a flexible, let javascript like Java new object out. I’m sorry to say, it’s just a SOP to C++,Java, C# programmers.”
5. Move closer to Java
“What is it?” said the little prince. Have you started using classes too? “” No, no, we provide something called a constructor. I’ll write some code for you, “the official said, dipping his drink again.
“Function already feels a bit like class,” said the little prince. “Oh dear, I see this. Did you capitalize Student on purpose?”
“Yeah, that makes it look like a Java class. But there’s a problem. Do you see it?”
The little prince thought for a moment. “Does that mean that every newly created object has a sayHello function? In Java, functions are defined on a class. If you define objects, that means you have a copy of every object, which is wasteful.”
“Yes, so we have to provide a more efficient way to put the sayHello function somewhere else!” “Where?” “Remember that prototype chain we were talking about? When an object calls a method, it goes up the chain, so we can create a prototype object with sayHello, and have Andy, Lisa, and other objects created from Student point to the prototype.”
“But all you have here is the constructor Student. Where do you create a prototype object? How do I point the proTO of objects like Andy and Lisa to prototype objects? You don’t want me to do it by hand.”
The optician glared at the little prince and said: “We in the javascript empire wouldn’t bother programmers so much. We could put this prototype object in the student. prototype property (not proto, mind you) so that every time you create an object like Andy or Lisa, Javascript automatically builds the prototype chain!”
“Oh, that is a little difficult to understand.” “Well, if I were to draw a picture, when you go to New Student, javascript will create this relationship chain:”
The little prince said: The Student constructor is actually a cover. Every time you go to New Student, you actually create an object (Andy or Lisa) and proto that object to the Student. Prototype object. That’s where the sayHello() method comes in.”
The glasses officer replied, “Yeah, the thing that’s confusing about this place is proto and Prototype. I don’t know why it was done in the first place, it’s just not elegant.”
“Yeah, the constructor and the Prototype concept are pretty convoluted, so we talked about providing a little syntax candy to lighten the programmer’s load.” “Agreed the robed officer.
Grammar sugar
The little prince was pleased to hear of syntax sugar, for Java also provides a lot of syntax sugar for programmers. The little prince was taken aback when the robed official wrote the javascript syntax:
This syntactic sugar has made javascript a lot like Java, C#, and C++ classes. It looks like the javascript empire has been working hard to “please” programmers.
The little prince now understands that Javascript is an object-oriented language based on prototype implementation, and there is no concept of classes at all. The new approach has brought a major shock to the little prince’s concept of thinking.
After staying here for a long time, he learned about the powerful functional programming of javascript and became more and more fond of javascript.
Will the little prince return to the Java Empire?