没有合适的资源?快使用搜索试试~ 我知道了~
首页纯JavaScript打造前端应用深度教程:零框架实践
纯JavaScript打造前端应用深度教程:零框架实践
需积分: 10 1 下载量 72 浏览量
更新于2024-07-16
收藏 1.1MB PDF 举报
"《使用纯JavaScript构建前端Web应用程序》(Gerd Wagner)是一本深入浅出的教程,作者Gerd Wagner通过五部分逐步教学,专注于不依赖任何第三方框架或库,利用JavaScript本身的强大功能来开发前端Web应用。这本书关注的重点在于工程化前端Web应用的实践,特别是利用浏览器内置的LocalStorage API进行数据存储,旨在帮助读者理解基础的前端编程原理。 本书的核心理念是,虽然现代Web开发通常会依赖各种框架,但掌握纯JavaScript可以增强开发者对底层技术的理解,并且在某些场景下,如小型项目或需要更直接控制的情况,使用纯JavaScript更为合适。作者强调,这是一本仍在草稿阶段的作品,可能存在错误和不完整性,鼓励读者在遇到问题时向他反馈,以便不断改进。 此版本的教材提供了PDF格式,可下载为"complete-tutorial.pdf",同时在项目页面<http://web-engineering.info/index.html>上也有更多信息。出版日期为2015年2月24日,版权归Gerd Wagner所有。所有的源代码遵循The Code Project Open License (CPOL),这意味着读者可以按照该许可协议自由使用和修改代码,只要保留适当的署名和版权信息。 通过阅读这本书,学习者将学到如何构建稳健的前端架构,编写高效的JavaScript代码,以及如何处理用户数据和实现本地持久化。这对于想要提升技术栈基础、理解核心Web技术的开发者来说,是一份宝贵的资源。"
资源详情
资源推荐
A Quick Tour of the
Foundations of Web Apps
5
<attachment file="lecture2.html" file="lecture3.html"/>
An XML document is called valid against a particular grammar (such as a DTD or an XML Schema), if
1.
it is well-formed,
2.
and it respects the grammar.
The evolution of HTML
The World-Wide Web Committee (W3C) has developed the following important versions of HTML:
• HTML4 as an SGML-based language (in 1997),
• XHTML 1 as an XML-based version of HTML4 (in 2000),
•
(X)HTML5 in cooperation (and competition) with the WHAT working group [http://
en.wikipedia.org/wiki/WHATWG] led by Ian Hickson [http://en.wikipedia.org/wiki/Ian_Hickson]
and supported by browser vendors (in 2014).
HTML was originally designed as a structure description language, and not as a presentation
description language. But HTML4 has a lot of purely presentational elements such as font. XHTML
has been taking HTML back to its roots, dropping presentational elements and defining a simple and
clear syntax, in support of the goals of
• device independence,
• accessibility, and
• usability.
We adopt the symbolic equation
HTML = HTML5 = XHTML5
stating that when we say "HTM"L or "HTML"5, we actually mean XHTML5
because we prefer the clear syntax of XML documents over the liberal and confusing HTML4-style
syntax that is also allowed by HTML5.
The following simple example shows the basic code template to be used for any HTML document:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8" />
<title>XHTML5 Template Example</title>
</head>
<body>
<h1>XHTML5 Template Example</h1>
<section><h1>First Section Title</h1>
...
</section>
</body>
</html>
HTML forms
For user-interactive web applications, the web browser needs to render a user interface. The traditional
metaphor for a software application's user interface is that of a form. The special elements for data
A Quick Tour of the
Foundations of Web Apps
6
input, data output and form actions are called form controls. An HTML form is a section of a document
consisting of block elements that contain controls and labels on those controls.
Users complete a form by entering text into input fields and by selecting items from choice controls.
A completed form is submitted with the help of a submit button. When a user submits a form, it
is sent to a web server either with the HTTP GET method or with the HTTP POST method. The
standard encoding for the submission is called URL-encoded. It is represented by the Internet media
type application/x-www-form-urlencoded. In this encoding, spaces become plus signs,
and any other reserved characters become encoded as a percent sign and hexadecimal digits, as defined
in RFC 1738.
Each control has both an initial value and a current value, both of which are strings. The initial value
is specified with the control element's value attribute, except for the initial value of a textarea
element, which is given by its initial contents. The control's current value is first set to the initial value.
Thereafter, the control's current value may be modified through user interaction or scripts. When a
form is submitted for processing, some controls have their name paired with their current value and
these pairs are submitted with the form.
Labels are associated with a control by including the control as a subelement of a label element
("implicit labels"), or by giving the control an id value and referencing this id in the for attribute of
the label element ("explicit labels"). Notice that implicit labels are, in 2014, still not well supported
by CSS libraries and assistive technologies. Therefore, explicit labels seem preferable, despite the fact
that they imply quite some overhead and repetitive code.
In the simple user interfaces of our "Getting Started" applications, we only need three types of form
controls:
1.
single line input fields created with an <input name="..." /> element,
2.
push buttons created with a <button type="button">...</button> element, and
3.
dropdown selection lists created with a select element of the following form:
<select name="...">
<option value="value1"> option1 </option>
<option value="value2"> option2 </option>
...
</select>
An example of an HTML form with implicit labels for creating such a user interface is
<form id="Book">
<p><label>ISBN: <input name="isbn" /></label></p>
<p><label>Title: <input name="title" /></label></p>
<p><label>Year: <input name="year" /></label></p>
<p><button type="button" id="saveButton">Save</button></p>
</form>
In an HTML-form-based user interface (UI), we have a correspondence between the different kinds of
properties defined in the model classes of an app and the form controls used for the input and output
of their values. We have to distinguish between various kinds of model class attributes, which are
typically mapped to various kinds of input fields. This mapping is also called data binding.
In general, an attribute of a model class can always be represented in the UI by a plain input control
(with the default setting type="text"), no matter which datatype has been defined as the range
of the attribute in the model class. However, in special cases, other types of input controls (for
instance, type="date"), or other controls, may be used. For instance, if the attribute's range is an
enumeration, a select control or, if the number of possible choices is small enough (say, less than
8), a radio button group can be used.
A Quick Tour of the
Foundations of Web Apps
7
JavaScript
This section provides a brief overview of JavaScript, assuming that the reader is already familiar with
basic programming concepts and has some experience with programming, for instance, in PHP, Java
or C#.
JavaScript is an object-oriented programming language that can be used for
1. Enriching a web page by
• generating browser-specific HTML content or CSS styling,
• inserting dynamic HTML content,
• producing special audio-visual effects (animations).
2. Enriching a web user interface by
• implementing advanced user interface components,
• validating user input on the client side,
• automatically pre-filling certain form fields.
3. Implementing a frontend web application with local data storage.
4. Implementing a frontend component for a distributed web application with remote data storage
managed by a backend component (server-side program).
Types and data literals in JavaScript
JavaScript has three primitive data types: string, number and boolean. There are three reference
types: object, array and function. Arrays and functions are just special kinds of objects. Types
are not declared and not checked since a JavaScript program is not compiled. Type conversion (casting)
is performed automatically.
The value of a variable may be
•
a data value: either a string, a number, or a boolean,
•
an object reference: either referencing an ordinary object, or an array, or a function,
• one of the following two special data values: undefined or null.
All numeric data values are represented in 64-bit floating point format with an optional exponent (like
in the numeric data literal 3.1e10). There is no explicit type distinction between integers and floating
point numbers. For making sure that a numeric value is an integer, or that a string representing a
number is converted to an integer, one has to apply the predefined function parseInt. If a numeric
expression cannot be evaluated to a number, its value is set to NaN ("not a number").
Like in Java, there are two pre-defined Boolean data literals, true and false, and the Boolean
operator symbols are the exclamation mark ! for NOT, the double ampersand && for AND, and the
double bar || for OR. When a non-Boolean value is used in a condition, or as an operand of a Boolean
expression, it is converted into a Boolean value according to the following rules. The empty string,
the (numerical) data literal 0, as well as undefined and null, are mapped to false, and all other
values are mapped to true.
For equality and inequality testing, always use the triple equality symbol === and !== instead of the
double equality symbol == and !=. Otherwise, for instance, the number 2 would be the same as the
string "2", since the condition (2 == "2") evaluates to true in JavaScript.
A Quick Tour of the
Foundations of Web Apps
8
Variable scope
In the current version of JavaScript, ECMAScript 5, there are only two kinds of scope for variables:
the global scope (with window as the context object) and function scope, but no block scope.
Consequently, declaring a variable within a block is confusing and should be avoided. For instance,
although this is a freqently used pattern, even by experienced JavaScript programmers, it is a pitfall
to declare the counter variable of a for loop in the loop, as in
function foo() {
for (var i=0; i < 10; i++) {
... // do something with i
}
}
Instead, and this is exactly how JavaScript is interpreting this code, we should write:
function foo() {
var i=0;
for (i=0; i < 10; i++) {
... // do something with i
}
}
All variables should be declared at the beginning of a function. Only in the next version of JavaScript,
ECMAScript 6, block scope will be supported by means of a new form of variable declaration with
the keyowrd let.
Strict Mode
Starting from ECMAScript 5, we can use strict mode [http://speakingjs.com/es5/
ch07.html#strict_mode] for getting more runtime error checking. For instance, in strict mode, all
variables must be declared. An assignment to an undeclared variable throws an exception.
We can turn strict mode on by typing the following statement as the first line in a JavaScript file or
inside a <script> element:
'use strict';
It is generallly recommended that you use strict mode, except your code depends on libraries that are
incompatible with strict mode.
Different kinds of objects
JavaScript objects are different from classical OO/UML objects. In particular, they need not
instantiate a class. And they can have their own (instance-level) methods in the form of method slots,
so they do not only have (ordinary) property slots, but also method slots. In addition they may also
have key-value slots. So, they may have three different kinds of slots, while classical objects (called
"instance specifications" in UML) only have property slots.
A JavaScript object is essentially a set of name-value-pairs, also called slots, where names can be
property names, function names or keys of a map. Objects can be created in an ad-hoc manner, using
JavaScript's object literal notation (JSON), without instantiating a class:
var person1 = { lastName:"Smith", firstName:"Tom"};
var o1 = {}; // an empty object with no slots
Whenever the name in a slot is an admissible JavaScript identifier [http://mothereff.in/js-variables],
the slot may be either a property slot, a method slot or a key-value slot. Otherwise, if the name is some
A Quick Tour of the
Foundations of Web Apps
9
other type of string (in particular when it contains any blank space), then the slot represents a key-
value slot, which is a map element, as explained below.
The name in a property slot may denote either
1.
a data-valued property, in which case the value is a data value or, more generally, a data-valued
expression;
or
2.
an object-valued property, in which case the value is an object reference or, more generally, an
object expression.
The name in a method slot denotes a JavaScript function (better called method), and its value is a
function definition text.
Object properties can be accessed in two ways:
1. Using the dot notation (like in C++/Java):
person1.lastName = "Smith"
2. Using a map notation:
person1["lastName"] = "Smith"
JavaScript objects can be used in many different ways for different purposes. Here are five different
use cases for, or possible meanings of, JavaScript objects:
1. A record is a set of property slots like, for instance,
var myRecord = {firstName:"Tom", lastName:"Smith", age:26}
2.
A map (or 'associative array') supports look-ups of values based on keys like, for instance,
var numeral2number = {"one":"1", "two":"2", "three":"3"}
which associates the value "1" with the key "one", "2" with "two", etc. A key need not be a valid
JavaScript identifier, but can be any kind of string (e.g. it may contain blank spaces).
3. An untyped object does not instantiate a class. It may have property slots and function slots like,
for instance,
var person1 = {
lastName: "Smith",
firstName: "Tom",
getInitials: function () {
return this.firstName.charAt(0) + this.lastName.charAt(0);
}
};
4. A namespace may be defined in the form of an untyped object referenced by a global object
variable, the name of which represents a namespace prefix. For instance, the following object
variable provides the main namespace of an application based on the Model-View-Controller
(MVC) architecture paradigm where we have three subnamespaces corresponding to the three parts
of an MVC application:
var myApp = { model:{}, view:{}, ctrl:{} };
5. A typed object o instantiates a class that is defined either by a JavaScript constructor function C
or by a factory object F. See ???
剩余148页未读,继续阅读
weixin_38743481
- 粉丝: 695
- 资源: 4万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 新型矿用本安直流稳压电源设计:双重保护电路
- 煤矿掘进工作面安全因素研究:结构方程模型
- 利用同位素位移探测原子内部新型力
- 钻锚机钻臂动力学仿真分析与优化
- 钻孔成像技术在巷道松动圈检测与支护设计中的应用
- 极化与非极化ep碰撞中J/ψ的Sivers与cos2φ效应:理论分析与COMPASS验证
- 新疆矿区1200m深孔钻探关键技术与实践
- 建筑行业事故预防:综合动态事故致因理论的应用
- 北斗卫星监测系统在电网塔形实时监控中的应用
- 煤层气羽状水平井数值模拟:交替隐式算法的应用
- 开放字符串T对偶与双空间坐标变换
- 煤矿瓦斯抽采半径测定新方法——瓦斯储量法
- 大倾角大采高工作面设备稳定与安全控制关键技术
- 超标违规背景下的热波动影响分析
- 中国煤矿选煤设计进展与挑战:历史、现状与未来发展
- 反演技术与RBF神经网络在移动机器人控制中的应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功