The difference between creating objects and Java objects to create a collection of stories in the form explain javaScript

Source: absolute beauty blog  
 
 

1 Introduction

As a Java  future heir to the empire, Java has been little prince rigorous education, not only proficient in the Java language, Java virtual machine , java libraries and frameworks, but also well aware of the various official Java specifications.

Cock wire counter-attack recently he heard of called Javascript, and successfully established an independent kingdom, not only became the front-end programming Wang, also continue to erode the territory Java empire!

According to the little prince palace teacher saying: I remember this guy just runs in the browser , the rub is completely Java development heat this up, but now it has come back to bully us, there is no justice endure what can not!

The Little Prince do not think that there must be reasonable, javascrip must be unique, the saying goes, know ourselves, know yourself, he felt the need to look at the Kingdom Javascript spy, gathering intelligence about to see this once the object-oriented browser language is how it is, why so many farmers rush yards.

2. Initial Impressions

After disguise, the little prince came to the Kingdom Javascript, this seems faction vibrant scene, one casual and unrestrained, unlike Java empire so serious and dull, feels feel good.

But the little prince was so incredible is that there is not even the official library of here! Tool to work with people of varied, dazzling, what AngularJS, React, Backbone, Vue, Ember, JQuery, ...... of each other inter also the tragedy, fight to fight, it is truly exciting.

Contrast This time, Java has a strict rule of the empire, has a huge library official, as well as the dominance of the Web framework SSH / SSM, along with a wide variety of Java specification, code farmers only need to bring learning, work on the line.

No selection worries, but also it reduces the right to choose, for better or worse the little prince himself does not know.

The Little Prince also noted that the Kingdom Javascript Few people write programs using the IDE, conveniently find a text editor can be started, and then thrown into the browser to run the test, really lightweight ah! Oh, we also Java Empire in the debate IntelliJ IDEA and Eclipse is better, it is not necessary ah.

3. There is no class how to create objects

As the investigation deepens, the little prince increasingly feel surprised, had no concept of class here! An object-oriented language there is not even like! This little prince and concepts are taught from birth but contrary!

No class how to create objects palace childhood teacher used to say: the first to write a class before a new object from this class.

But here but has numerous javascript object, they continue to produce, dying, hard to work with, support from a large, vibrant empire.

These objects come from the little prince puzzling coincided with noon, the little prince saw in front of a JSON pub, I decided to take a rest, eat a meal Mimi say.

To the little prince pounds of cooked beef, the three bowls of wine, was about to enjoy, only to hear the next table wearing a robe who asked: hey, what you said that the prototype object is

Another is wearing glasses whispered: Hush, silence, just the king issued a decree prototyping is the secret of our empire, prohibiting public discussion, to avoid being Java empire to learn to go.

Little Prince heart of a dynamic, small two immediately called, to come to the finest food and wine, to the next table, asked the two taken to drink. Some drink down, the little prince finally got the initial trust of the two, had they javscript officials in charge of reviewing the language specification.

The little prince asked: "My generation business, travel extensively, visited C ++ Kingdom, Java Empire, C # Empire, they are known as object-oriented language, there are class and object distinction, but to let javascript kingdom, how I could not even a class did not see ah. "

Officials glasses, said: "We do not have class, that stuff too straightforward!"

Little Prince secretly amazed, but think carefully, as if that is so, ah, like when I had to learn Java, took great trouble accepting the concept of a class, in fact, object-oriented system , not that interactions between objects to do it like

Then the little prince asked a key question: "There is no class, how to create objects ah."

"Stranger, is not so complicated, you think about what is the object ah, no way to do this is to add attributes which you create an object we look at it," the official said, and wrote a finger dipped in wine on the table stand up:

\

See not, this animal object defines a property name, and a way to eat, it is simple "

Indeed simple and clear, all without class, an object is created, in front of the little prince seemed to open up a new door.

"Since the object and not class association, we are free to increase the object attributes:" glasses to the official added.

\

"So can play!" The little prince to be scared, there is no constraint class, these objects are also too free now.

4. There is no class how to inherit

"That's how to achieve inheritance, object-oriented inheritance but important concept ah"

Glasses official said: "simple, ah, is to let the two objects do not inherit associate javascript thing in our kingdom, each object has a special property called __proto__, you can use this property to associate another object (the object! is called a prototype), I'll draw you to it. "

\

This wine written code is not long, but it deeply shocked the little prince, because the amount of information is enormous, hidden secret "prototype" of the little prince could not help but fall into a deep thought:

The prototype is the object dog animal (note: also an object), the prototype object of cat is animal.

Whether dog or cat, are not defined eat () method, then how you can call it

When the eat method is called, first in their own way to find the list, if not, went to the prototype method, if the prototype is not found, the prototype of the prototype went to look for ..... Finally Object found there, if it can not find, and that is the undefined.

Here these objects must be established by __proto__ a prototype chain!

\

嗯, 我师父给我讲JVM虚拟机的时候, 也提到了一个对象在执行方法的时候,需要查找方法的定义,这个查找的次序也是先从本对象所属的类开始, 然后父类, 然后父类的父类...... 直到Object, 思路是一模一样的!

只不过Java 的方法定义是在class中, 而这个javascript 的方法就在对象里边, 现在我觉得似乎在对象里更加直观一点啊。

属性和方法应该类似,也是沿着原型链向上查找, 不过这里dog的name属性似乎覆盖了animal的name属性, 还有那个this, 在调用dog.eat()的时候,应该是指向dog这个对象的。

看来面向对象的理念都是想通的啊。 想着想着,小王子脸上竟然露出了笑容。

看到小王子像程序卡住一样,不动了, 穿长袍的官员推了小王子一把: 外乡人, 你怎么了

小王子意识到自己的失态, 赶紧说: “哦,没啥, 我觉得你们使用的这个’原型‘的办法很精妙啊, 完全不用类就实现了继承。”

眼镜官员一愣: “外乡人, 看来你悟性不错, 帝国的秘密已经被你给洞察了, 不过很多新来的程序员就不容易体会到这一点, 于是我们就做了一个变通, 让javascript可以像Java那样new 出对象出来。说来惭愧, 这完全是为了迁就那些C++,Java, C#程序员啊 ”

5. 向Java靠拢

小王子说:”什么变通办法 难道你们也开始使用类了吗“

“不不, 我们提供了一个叫做构造函数的东西。还是给你写点儿代码吧 ” 官员说着,又蘸着酒水写了起来:

小王子说道: “那个function 已经有点 class的感觉了啊, 天呐我竟然看到了this这个关键字, 对了那个Student是你故意写的大写吗 ”

“是啊 , 这样以来看起来就像Java的类了。但是,中间有个问题,你看出来了吗 ”

小王子想了一阵:“ 是不是说每个新创建对象都有一个sayHello函数 在Java中函数都是定义在class 上的。 如果定义对象上, 那就意味着每个对象都有一份, 太浪费了。”

“是的,所以我们得提供一种更加高效的办法, 把这个sayHello函数放到另外一个地方去! ”

“放到哪里 ”

“记得我们刚才说的原型链吗 当一个对象调用方法的时候,会顺着链向上找,所以我们可以创建一个原型对象,其中包含sayHello函数, 让andy, lisa这些从Student创建起来的对象指向这个原型就ok了。”

“可是你这里只有构造函数Student, 在哪里创建原型对象呢 怎么把andy,lisa 这些对象的__proto__指向原型对象呢 不会让我手工来指定吧。”

眼镜官员瞪了一眼小王子说: “我们javascript帝国肯定不会这么麻烦程序员的, 我们可以把这个原型对象放到Student.prototype这个属性中(注意,不是__proto__), 这样一来,每次当你创建andy,lisa这样的对象时, javascript 就会自动的把原型链给建立起来!”

\

小王子面露难色:“唉,这理解起来有点难啊。”

"还是画个图吧, 当你去new Student的时候,javascript会建立这样的关系链:"

\

小王子说: “明白了,这个所谓的构造函数Student 其实就是一个幌子啊, 每次去new Student的时候,确实会创建一个对象出来(andy或者lisa) , 并且把这个对象的原型(__proto__)指向 Student.prototype这个对象,这样一来就能找到sayHello()方法了。”

眼镜官员回答:“没错,这个地方容易让人混淆的就是__proto__和prototype这两个属性, 唉,我也不知道最早为什么这么干, 实在是不优雅。”

“是啊,这个构造函数再加上prototype的概念,实在是让人费解, 所以我们商量着提供一点语法糖降低程序员的负担。” 长袍官员附和到。

6. 语法糖

听到语法糖,小王子觉的很亲切, 因为Java 中也提供了很多方便程序员的语法糖。

当长袍官员写出javascript的语法糖的时候, 小王子不由得大吃一惊:

This javascript syntactic sugar've become very much like Java, C #, C ++ classes, it seems Empire javascript in order to "please" programmers, efforts have been made in changing our java empire seems to have to refuel ah.

The little prince now understand that Javascript is a prototype-based object-oriented language implementation, there is no concept of class, a new way of thinking to the idea of ​​the little prince has brought great impact.

Stay here for a long time, he learned javascript powerful functional programming, more and more like javascript, a little reluctant to leave.

Guess you like

Origin www.cnblogs.com/yanyunpiaomaio/p/11025444.html