首页 > 编程语言 >An Introduction to JavaScript

An Introduction to JavaScript

时间:2023-01-11 21:01:52浏览次数:57  
标签:do 浏览器 language Introduction JavaScript HTML browser

An Introduction to JavaScript

An Introduction to JavaScript

Let’s see what’s so special about JavaScript, what we can achieve with it, and what other technologies play well with it.

让我们看看JavaScript的特别之处,我们能用它实现什么,以及哪些技术能够与之产生奇妙搭配。

What is JavaScript?

JavaScript was initially created to “make web pages alive”.

The programs in this language are called scripts. They can be written right in a web page’s HTML and run automatically as the page loads.

Scripts are provided and executed as plain text. They don’t need special preparation or compilation to run.

In this aspect, JavaScript is very different from another language called Java.

JavaScript最开始被创造是为了“使web页面生动起来”。

这种语言变成被叫做脚本。它们能被正确写入web页面的HTML里并且能子啊页面加载时自动运行。

脚本以纯文本格式被提供与执行。他们不需要特殊的准备或者编译去执行。

这方面,JavaScript与另一门语言Java非常不同。

Why is it called JavaScript?

When JavaScript was created, it initially had another name: “LiveScript”. But Java was very popular at that time, so it was decided that positioning a new language as a “younger brother” of Java would help.

But as it evolved, JavaScript became a fully independent language with its own specification called ECMAScript, and now it has no relation to Java at all.

Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called the JavaScript engine.

The browser has an embedded engine sometimes called a “JavaScript virtual machine”.

Different engines have different “codenames”. For example:

  • V8 – in Chrome, Opera and Edge.
  • SpiderMonkey – in Firefox.
  • …There are other codenames like “Chakra” for IE, “JavaScriptCore”, “Nitro” and “SquirrelFish” for Safari, etc.

The terms above are good to remember because they are used in developer articles on the internet. We’ll use them too. For instance, if “a feature X is supported by V8”, then it probably works in Chrome, Opera and Edge.

JavaScript创建之初,有另一个名字:"LiveScript"。但当时Java异常流行,因此将这门新语言定位为Java"弟弟"有助于流行。

但是随着发展,JavaScript成为拥有着自己的语言规范(ECMAScript)的完全独立的一门语言,现在它和Java没有任何关系了。

今天,JS不仅能运行在浏览器上,也可以在服务端,甚至在任何拥有JavaScript引擎的设备上执行。

浏览器内嵌了引擎,有时候也被称之为“JavaScript虚拟机”。

不同引擎有不同的“代号”。比如:

  • V8—Chrome,Opera和Edge
  • SpiderMonkey —Firefox
  • ……还有其他一些代号,像 IE的“Chakra”,“JavaScriptCore”、“Nitro” 和 Safari的“SquirrelFish” ,等等。

上面这些术语很好记,因为它们现在被用于网上开发者的文章中。我们也会用到。例如,如果说“V8支持某个功能”,这或许意味着它在Chrome\Opera\Edge上能运行。

How do engines work?

Engines are complicated. But the basics are easy.

  1. The engine (embedded if it’s a browser) reads (“parses”) the script.
  2. Then it converts (“compiles”) the script to machine code.
  3. And then the machine code runs, pretty fast.

The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and further optimizes the machine code based on that knowledge.

引擎是复杂的。但是基本原地很简单。

  1. 引擎(浏览器内嵌)读取(”解析“)脚本。
  2. 转换(“编译”)脚本为机器语言。
  3. 机器代码快速执行。

引擎会对流程的每一步进行优化。它甚至在编译的脚本运行时监视她,分析流经它的额数据,基于获得的信息进一步优化机器代码。

What can in-browser JavaScript do?

Modern JavaScript is a “safe” programming language. It does not provide low-level access to memory or the CPU, because it was initially created for browsers which do not require it.

JavaScript’s capabilities greatly depend on the environment it’s running in. For instance, Node.js supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc.

In-browser JavaScript can do everything related to webpage manipulation, interaction with the user, and the webserver.

For instance, in-browser JavaScript is able to:

  • Add new HTML to the page, change the existing content, modify styles.
  • React to user actions, run on mouse clicks, pointer movements, key presses.
  • Send requests over the network to remote servers, download and upload files (so-called AJAX and COMET technologies).
  • Get and set cookies, ask questions to the visitor, show messages.
  • Remember the data on the client-side (“local storage”).

现代JavaScript是一门“安全的”语言。它并不提供对内存或者CPU的底层访问,因为它最初是为浏览器创造的,不需要这些。

JavaScript的能力非常依赖于所运行的环境。例如,Node.js支持函数允许JavaScript读写任意文件,执行网络请求等等。

在浏览器中,JavaScript能做任何与网页操作、用户交互和web服务器有关的事情。

例如,浏览器中的JavaScript可以:

  • 在页面中增加新的HTML,改变已有内容,定义样式。
  • 对用户行为作出反应,在鼠标点击、焦点移动、键盘键入时运行。
  • 向远程服务器发送请求,下载文件和上传文件(被称为AJAX和COMET技术)。
  • 获取并设置cookies,向访问者提问、发送消息。
  • 在客户端记住数据(“localStorage”)。

What CAN’T in-browser JavaScript do?

JavaScript’s abilities in the browser are limited to protect the user’s safety. The aim is to prevent an evil webpage from accessing private information or harming the user’s data.

Examples of such restrictions include:

  • JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS functions.

    Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like “dropping” a file into a browser window or selecting it via an <input> tag.

    There are ways to interact with the camera/microphone and other devices, but they require a user’s explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the NSA.

  • Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other page if they come from different sites (from a different domain, protocol or port).

    This is called the “Same Origin Policy”. To work around that, both pages must agree for data exchange and must contain special JavaScript code that handles it. We’ll cover that in the tutorial.

    This limitation is, again, for the user’s safety. A page from http://anysite.com which a user has opened must not be able to access another browser tab with the URL http://gmail.com, for example, and steal information from there.

  • JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that’s a safety limitation.

Such limitations do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugins/extensions which may ask for extended permissions.

为了保护用户的信息安全,浏览器中的JavaScript有所限制。目的是防止恶意网站窃取私人信息或者损害用户数据。

类似限制的例子包括:

  • 网页上的JavaScript不能读写、复制和执行硬盘上的任意文件。它没有直接访问操作系统的功能。

    现代浏览器允许js做一些与文件相关的操作,但是受到一定的限制。只有在用户做了特定行为,比如“拖放”一个文件进入浏览器窗口或者通过一个<input>标签选择文件时,js才能操作这个文件。

    有很多方式与相机/麦克风等其他设备交互的方式,但是他们需要用户明确的许可。因此,启用了js的网页应该不会偷偷的启动网路摄像头观察周围,然后把信息发送到NSA。

  • 不同标签页/窗口之间通常互不了解。有时候会有一些联系,例如当一个窗口使用js打开另一个窗口时。但即便是在这个例子中,来自一个页面的js如果与另一个页面来自不同的网址(来自不同的域名、协议或者端口),那么它也不能进行通信。

    这被称为“同源策略”。为了解决这个问题,两个页面必须都统一数据交换而且必须包含为了处理此问题的特定js代码。我们会在本教程中包含这些内容。

    这个限制也是为了用户的信息安全。用户已经打开的http://anysite.com页面必须不能访问另一个浏览器页签的http://gmail.com网页,也不能从那里窃取信息。

  • JavaScript可以轻松与当前网页所在的服务器进行通信。但是它从其它网址/域的能力被削弱了。尽管这是可能的,但是他需要(在HTTP header中)获得远程服务器的明确同意。再次重申,这是一个安全限制。

类似的限制会在当我们在浏览器之外使用JavaScript时就不存在了,比如在一个服务器上。现代浏览器页也允许安装可能会要求额外权限的插件/拓展。

What makes JavaScript unique?

There are at least three great things about JavaScript:

  • Full integration with HTML/CSS.
  • Simple things are done simply.
  • Supported by all major browsers and enabled by default.

JavaScript is the only browser technology that combines these three things.

That’s what makes JavaScript unique. That’s why it’s the most widespread tool for creating browser interfaces.

That said, JavaScript can be used to create servers, mobile applications, etc.

关于JavaScript至少有三个闪光点:

  • 与HTML/CSS完全集成
  • 易写
  • 所有主流浏览器支持且默认开启

JavaScript是唯一将以上三点结合的浏览器技术。

这使JavaScript变得独特。也是为什么他是创造浏览器姐买你最广泛的工具。

此外,JavaScript也能本用于创造服务、移动端应用等。

Languages “over” JavaScript

The syntax of JavaScript does not suit everyone’s needs. Different people want different features.

That’s to be expected, because projects and requirements are different for everyone.

So, recently a plethora of new languages appeared, which are transpiled (converted) to JavaScript before they run in the browser.

Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it “under the hood”.

Examples of such languages:

  • CoffeeScript is “syntactic sugar” for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
  • TypeScript is concentrated on adding “strict data typing” to simplify the development and support of complex systems. It is developed by Microsoft.
  • Flow also adds data typing, but in a different way. Developed by Facebook.
  • Dart is a standalone language that has its own engine that runs in non-browser environments (like mobile apps), but also can be transpiled to JavaScript. Developed by Google.
  • Brython is a Python transpiler to JavaScript that enables the writing of applications in pure Python without JavaScript.
  • Kotlin is a modern, concise and safe programming language that can target the browser or Node.

There are more. Of course, even if we use one of these transpiled languages, we should also know JavaScript to really understand what we’re doing.

JavaScript的语法不能满足所有人。不同的人想要不同的功能。

这可以理解,因为每个人的项目和需求都不一样。

所以最近出现了了一系列新语言,在被运行到浏览器之前这些语言会被编译成JavaScript。

现代工具是的编译又快又清晰,事实上允许开发者使用另一种语言编码然后底层自动转换为JavaScript。

例如这些语言:

  • CoffeeScript是JavaScript的语法糖。它引入更短的语法,允许我们编写更简洁明了的代码。通常Ruby开发者喜欢它。
  • TypeScript聚焦于增加“严格数据类型”来简化开发、支持复杂系统。由微软开发。
  • Flow也增加了数据类型,但是采用了不同的方式。由Facebook开发。
  • Dart是一门独立的语言,他有着自己的引擎,运行在像手机应用这样的无浏览器环境,但也能被编译成javaScript。由谷歌开发。
  • Brython是一个Python到JavaScript的编译器,能不使用JavaScript以纯Python语言编写应用。
  • Kotlin是一个现代的、简洁且安全的编程语言,运行环境是浏览器和Node。

还有很多。当然,即使我们使用类似的编译语言中的一个,我们也应该了解JavaScript,这样才能理解我们再做什么。

Summary

  • JavaScript was initially created as a browser-only language, but it is now used in many other environments as well.
  • Today, JavaScript has a unique position as the most widely-adopted browser language, fully integrated with HTML/CSS.
  • There are many languages that get “transpiled” to JavaScript and provide certain features. It is recommended to take a look at them, at least briefly, after mastering JavaScript.
  • JavaScript最初被创造于仅浏览器环境,但现在能被用在很多其他环境。
  • 时至今日,JavaScript作为最广泛使用的浏览器语言,且与HTML/CSS完全集成,有着独特的地位。
  • 有许多语言能被编译成JavaScript并且提供更多的功能。建议最好了解下他们,至少可以在掌握Ja v aScript之后大概了解下。

标签:do,浏览器,language,Introduction,JavaScript,HTML,browser
From: https://www.cnblogs.com/shixiu/p/17044886.html

相关文章

  • 7.JavaScript--正则表达式
    实验原理正则表达式是用于处理字符串的强大工具,其他编程语言中也有正则表达式式的概念,区别只在于不同的编程语言实现支持的语法数量不同。它拥有自己的独特的语法以及一个......
  • JavaScript严格模式(use strict)
    一、什么是严格模式(strictmode)JavaScript严格模式即在严格模式下运行。严格模式下,你将不能使用未声明的变量。注意,严格模式需要浏览器的支持:Internetexplorer10+、Fire......
  • JavaScript超大文件上传
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持......
  • JavaScript – Sort
    前言写程序难免会遇到需要排序的时候.但看是简单的排序,其实暗藏杀机.一不小心就会搞出Bug哦.这篇就简单聊聊JS的排序 ><大过小过排序自然就是先比大小咯.......
  • JavaScript每次刷新照片都不一样
    每次刷新背景图片都不一样    下面是vue的,不过逻辑都一样,只是定义方式不同而已 data是这个//随机数生成randomNumber:'', 这个是在metho......
  • JavaScript 图像压缩
    JavaScript可以使用类似于canvas和webworkers来实现图像压缩。使用canvas,可以将图像绘制到canvas上,然后使用canvas提供的toBlob()或toDataURL()方法将其转......
  • 【javascript】关于charCodeAt()方法
    在做算法题目leetcode2283时,看见某些答案会使用charCodeAt(),因为自己没用过,所以作此纪录描述在JavaScript中,charCodeAt()是一种字符串方法,用于检索字符串中特定位置的......
  • JavaScript所有内部属性列表 [[Configurable]] 等
    简介据MDN文档所说内部属性是由[[···]]包裹的内容,于是我们去复制ECMA-262标准的所有文字部分,然后用正则统计[[···]]的出现并打印效果代码经老大提醒,这里......
  • JavaScript学习笔记—运算符
    运算符(操作符):可以用来对一个或多个操作数(值)进行运算1.算术运算符:+加法运算符-减法运算符*乘法运算符/触发运算符**幂运算%模运算算术运算时,除了字符串的加......
  • JavaScript 错误 throw、try、catch
    JavaScript错误throw、try、catchtry:语句测试代码的错误;catch:语句处理错误;throw:语句创建自定义错误;finally:语句在try和catch语句之后,无论是否有触发异常,该语句都会执行......