Written in the book of the former
Life is short. I use JavaScript. However, there is a popular saying in other circles: Life is too short, I use Python. Of course, there is also the perfect hair programming, which is not mentioned. When you get to a certain point on the front end, you kind of want to go out and have a look, go over the mountain, go out of the mountain, take a look at the landscape. Although the scenery outside is just like that, but I can’t help but have a look (%E8%B4%B1%E5%91%97%0A).
Many say
As a restless FEE, I said I would learn even if I forgot. Learned and forgotten, and never learned, are completely different concepts. I remember there is an idiom like this, called by analogy.
The way we think about it is, let me take an example, let’s say I learned JavaScript, and I learned JavaScript to become familiar with other languages like this. But I think we can also understand it in another way, for example, I’m good at JavaScript, I want to learn Python, and I want to complement my JavaScript by learning Python, and by learning Python, Made me think more deeply about JavaScript. For example, keywords like import have long been available in Python. In fact, what I want to say is that the more you learn, the more you know, and the higher you look at the pattern of things, you will find that a lot of problems can be solved by changing everything. B: well… This is not more than one sentence, it is clearly a paragraph…
Yeah, yeah, it’s time to explore!!
From the same family
I like to compare what I’ve learned to what’s on the front end. After studying Python systematically (watching MOOCs videos and reading good books). In fact, the harvest is quite a lot.
PY and JS have a lot in common, but the most important thing in common is that JS and PY are interpreted languages. Interpretive language commonly known as scripting language, open the history of programming language, will find that the birth of Perl language is a sign of scripting language maturity, interested can consult information to understand, if you want to in-depth understanding of the principle of programming language, you can see “programming language principle” 10th edition, the author is Sebastian. Very well written! Don’t ask me why, because I’ve seen it before, lol.
The essential difference between interpreted languages and compiled languages like Java is the following:
Interpreted languages do not need to be compiled by the developer and are translated into machine language at run time. Java is a language that requires developers to manually compile, which is better, in fact, there is no way to compare, when it comes to performance, it must be Java performance is better, because after a compilation, the subsequent multiple runs can directly run bytecode files, no need to compile again. And JavaScript, Python, although the performance is a little bit poor, every run needs to be compiled, but the development efficiency is high ah, portability is also very good. Give it a rumtime and it will send you a happy life. Besides, I’m a scripting language. What am I doing slow? Have you ever heard of a famous saying in computer science:
Any problem in computer science can be solved by adding an indirect intermediate layer.
According to this theory, it is possible to find a way to solve the problem of interpreted languages running slowly. This is called JIT compilation, which means just-in-time compilation, using an intermediate layer to save translated machine code and then execute it directly from the cache the next time it is called.
JS has been absorbing the advantages of PY
As a leader in the scripting industry, PY has always played a noble role. PY’s design philosophy is elegance, clarity, and simplicity. And looking back at the original use of 10 days to invent JS, I found JS just like a self-made diaosi (people poor ambition is not poor). Through the method of encircling the city by the countryside, step by step, and finally kill a piece of their own sky.
Why do you say so, because of my personal understanding, JS when being created, not giving too much expectation, this also caused a lot of things doesn’t take into account when created, such as the JS is designed based on the face object, but there was no class, inheritance and so on in the face of the object must have a feature of a language. PY has been a face-to-object language since it was designed. For example, JS in this, there are a variety of points, for the beginning of the front end of the students, is a difficult to understand the knowledge point. Throughout OOP(object facing programming) languages, this is explicitly pointed to objects created by classes. In JS, it is because of the ambiguous positioning at the beginning of the design, OOP, lack of class and other features, but also based on the face of the object design.
Since there is no language design level to give the corresponding keywords, such as the class keyword, if there is no class, that JS and what, it is a function of the ya 😂, OOP is not obvious, that the rest can only OPP(process oriented) 😂. In this way, the direction of this will become a variety of situations, I will not say the specific situation, check the data by myself. But it’s time for a quote:
Any problem in computer science can be solved by adding an indirect intermediate layer.
You said there is no class keyword? Can’t implement inheritance? Can’t achieve polymorphism? It doesn’t exist. I’d starve to death, and I’d make it happen. B: well… Then there is the use of prototype chains to implement inheritance in the beginning, and to reduce memory consumption, can be optimized to composite inheritance. In fact, all of these are to fill the original design when there is no good planning pit, former colleagues dug pits, I fill. Later, when ES6 comes out, you can finally write OOP using class. But this class is only grammar sugar, to put it bluntly, the pit left by colleagues, has been deep in the center of the earth! It’s all over the world. What can I do? I will create an indirect middle layer to implement the class function. I will leave the rest of the bottom layer untouched. After all, users are all over the world. So over time, you build an intermediate layer, I build an intermediate layer, and then JS becomes more and more elegant, clear, concise. The days are getting better and better.
Under the efforts of FEE all over the world, JS has been making efforts to advance in the direction of elegance, clarity and simplicity.
How does JS absorb PY
Star suction method is also divided into grades, suction is not good, may explode.
The first thing about semicolons:
It is standard to write PY code without a semicolon and to use a newline as the end of the line. At present, the mainstream framework of JS advocates no semicolon. The goal is simple: simple and efficient. JS future standard.
The second one is about keywords
Both PY and JS use import as a module import keyword
The third one is about functions
Both have closures, anonymous functions, and both can use Lamada expressions.
For the record, there are other languages that have PY as well, but the reason I’m learning PY is to complement JS, so let me look at my cute JS from a different Angle. In fact, on the front end, a lot of people don’t understand closures. Knowing what is, not knowing why. Why does this happen?
In my opinion, the main reason is that closures play a very important role in JS language. To put it more simply, there are so many places and scenes that use closures, many of which have to be completed using closures, and the functions of closures in many scenarios are also different. Many just entered your circle not long baby, still only stay in a very shallow understanding. For example, prevent global variable pollution, modularization and so on. The important role of closures in JS is not deeply understood. To thoroughly understand closures, the principles of the compiled language must be understood or understood.
How important closures are in JS, I personally think: closures exist in many languages, but JS relies on closures more than any other language does. Why do you say that? Let’s make it a little bit bigger: from a programming language perspective, closures are:
A subroutine and the reference environment that defines it. That is, if a subroutine can be called from anywhere in the program, it needs to reference the environment.
Stop and look beyond closures for a moment, and you’ll notice that there’s a strange word called reference environment. In fact, the reference environment here is also called scope. Just a different name, so the question is, what is a reference context (scope)?
The reference context (scope) is the set of all visible variables in the statement.
To understand this sentence, it is very important for you to understand the scope often mentioned in the front end. Here is a very simple code:
const g = 'haha'
const a = 'i am godkun'
function fun() {
let b = 'hello world'
console.log(b + a)
}
fun()
Copy the code
OK, look at the code above, what is the scope of the fun() statement? Features, according to the language of the static scope: are you going to fun function code execution place to see, through the fun function can know, fun function (reference environment) is the scope of the fun function in vivo of the statement of the local scope of variables b and under the global scope of a collection of emphasize here, even if a global variable g has not been fun function use. That’s also counted in the fun reference context.
To sum up: in statically scoped languages, a statement’s reference environment is the set of variables declared in its local scope and all visible variables declared in its ancestor scope.
Okay, moving on to closures, which are useless if static scoped programming languages don’t allow nested subroutines. If nested subroutines are allowed, closures are supported. In languages that allow nested subroutines, all variables (their local and global variables) referenced by the subroutine in the environment are accessible, regardless of where the subroutine is invoked in the program. This sentence is not easy to understand, we can use JS as an example to carry out popular interpretation:
JS is a static scoped language, while allowing nested subroutines, if JS does not allow nested subroutines, that cool cool to small cute JS ah. What is static scoped language, the official solution will not say, I popular point to say, is that your JS program in the declaration, has already determined the scope. For those of you who have studied the principles of programming languages, static scopes are also called lexical scopes, and the variables that use lexical scopes are called lexical variables.
function say() {
let str = 'hello world'
console.log(str)
}
Copy the code
As you can see from above, the variable STR is a lexical variable. So the core essence is going to come out.
Lexical variables have a defined scope and an uncertain lifetime.
The scope of a lexical variable can be a function or block, making it valid within the code area. Since JS supports block-level scopes (let declarations don’t really count as block-level scopes, anyway), this block has become a reality. The lifetime of a lexical variable depends on how long it needs to be referenced. How long it takes is something we can artificially control. And artificial control is always unreliable. So the GC was born.
As you can see from the static scope above, there should also be a dynamic scope. What is the dynamic scope? Forget it! After all, has nothing to do with JS. I just said I was doing it! Speaking of which, should I write a closure article? Forget it.
I found I couldn’t finish
I wanted to finish the article, but I found it was too much to stop writing. Now I have to attend VueConf at 9:00 today (1542994654097), and I have to attend Google Developer conference in Shanghai tomorrow. And then the excitement is that today I have to get up at 5 am to do the high-speed train, well, silently added at the end of the article title – last article, I’d better stop sleeping.
In fact, the purpose of learning PY is to complement JS, to see JS through PY. In fact, learning the server side language, there is a very important help to the front end, is to deeply understand the Web programming about a lot of knowledge of the network, TCP/IP, Socket, HTTP and so on, as well as in the Web security aspect, the server side is how to do. Wait, I will talk about it in the next article, and of course I learned PY after a small actual combat, also can be regarded as learning useful bar. It’s not useful, but it’s fun.
Node.js is also a server-side language, so why not learn Node.js? For example, closures are widely used in Node.js source code, and closures are used in asynchronous IO operations. But node.js is also written in JS 😂, I am a curiosity series, so Node.js does not count.
PY has decorators, JS doesn’t, but the great translator Babel solves this problem, and I think it’s conceivable that Babel could one day even unify scripting languages. Babel then analyzes existing code, parses it into an AST, transforms it into a new AST, and then asks the interpreter to interpret the new code structure.
And then, you know, from the command to compile the scripting language you want. PY and JS are combined to form:
Life is short and I love Photoshop. pawn
Dude, if you love Photoshop, just give it a thumbs up, lol. Welcome to pay attention to the follow-up series will be more wonderful 😊!