Eloquent JavaScript #02# program_structure
第一章中作者介绍了各种值,但是这些独立的值是没有意义的,只有当值放在更大的框架的时候才会彰显它们的价值。所以第二章开始介绍程序结构。
1、var VS. let 以及 const
作者推荐用 let ,因为var 有一些稀奇古怪的行为?暂时没有详细解释。
const 用于绑定常量值
2、关于 JavaScript 的运行环境
在运行 JavaScript 程序的环境中,并不仅仅只有你定义的绑定,还有其它各式各样初始化就有的“环境绑定”。
例如说 prompt , 这是一个持有函数类型的绑定,不过在现代浏览器里几乎不用了,理由是它没法装饰。。不过在玩具式程序里倒是用得很多。
typeof prompt
"function"
typeof NaN
"number"
3、关于自增号。
在 JavaScript 里 ++ -- += -= *= 都是可以用的,但是在 Python 里不行,它有 += 却没有 ++ --
4、javascript 命名规范
遵循驼峰命名法(可以发现 JavaScript 标准函数就是那样命名的, 但为什么像 Number 那样的函数第一个字母是大写呢?因为是它是构造器)
Number("xxxx")
NaN
Number("222")
222
5、打印下面这个图形:
#
##
###
####
#####
######
#######
let result = "#";
for (let i = 0; i != 7; ++i) {
console.log(result);
result += "#";
}
6、关于控制流的小练习
Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print "Fizz" instead of the number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.
When you have that working, modify your program to print "FizzBuzz" for numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz" for numbers divisible by only one of those).
<script type="text/javascript">
for (let i = 1; i <= 100; ++i) {
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else {
console.log(i);
}
}
</script>
PS. 优先级 + - 等远大于 == != 远大于 &&和||(其中 && 大于||) 赋值号优先级是最小的,另外,上面的 === 属于多此一举,当可以确定两边值类型相同的时候用 == 就可以了。
7、练习三:
Passing this string to console.log should show something like this:
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
When you have a program that generates this pattern, define a binding size = 8 and change the program so that it works for any size, outputting a grid of the given width and height.
<script type="text/javascript">
let size = Number(prompt("Enter the size of chessboard"));
if (!Number.isNaN(size)) {
// NaN 虽然含义是 "Not a Number",不过 typeof 输出类型仍然是 Number
// 另外,意外发现 IE 环境居然不支持 isNaN 属性
let result = "";
for (let i = 0; i != size; ++i) {
for (let j = 0; j != size; ++j) {
if ((i + j) % 2 == 0) {
result += " ";
} else {
result += "#";
}
}
result += "\n";
}
console.log(result);
}
</script>
Eloquent JavaScript #02# program_structure的更多相关文章
- Eloquent JavaScript #13# HTTP and Forms
索引 Notes fetch form focus Disabled fields form’s elements property 阻止提交 快速插入单词 实时统计字数 监听checkbox和rad ...
- 前端面试准备笔记之JavaScript(02)
01. this的典型应用场景 this在各个场景中取什么值,是在函数执行的时候确认的,不是在定义的时候确认的. 普通函数执行 返回window function fn1() { console.lo ...
- Eloquent JavaScript #11# The Document Object Model
索引 Notes js与html DOM 在DOM树中移动 在DOM中寻找元素 改变Document 创建节点 html元素属性 布局 style CSS选择器 动画 Exercises Build ...
- Eloquent JavaScript #10# Modules
索引 Notes 背景问题 模块Modules 软件包Packages 简易模块 Evaluating data as code CommonJS modules ECMAScript modules ...
- Eloquent JavaScript #04# Objects and Arrays
要点索引: JSON More ... 练习 1.补:js字符串的表达方式有三种: "" 和 '' 没什么区别,唯一区别在于 "" 中写 "要转义字符 ...
- Eloquent JavaScript #03# functions
索引: let VS. var 定义函数的几种方式 more... 1.作者反复用的side effect side effect就是对世界造成的改变,例如说打印某些东西到屏幕,或者以某种方式改变机器 ...
- Eloquent JavaScript #01# values
When action grows unprofitable, gather information; when information grows unprofitable, sleep. ...
- 前端之JavaScript 02
一.函数 // 最基础的函数定义 function f1() { console.log('hello world!'); } f1(); // hello world! // 带参数的函数 func ...
- JAVA企业级开发-JavaScript(02)
一.JavaScript介绍 Javascript语言诞生主要是完成页面的数据验证.因此它运行在客户端,需要运行浏览器来解析执行JavaScript代码. 特点: 交互性(它可以做的就是信息的动态交互 ...
随机推荐
- Python pickle 模块
转自:https://www.cnblogs.com/lincappu/p/8296078.html pickle可以存储的数据类型 所有python支持的原生类型:布尔值,整数,浮点数,复数,字符串 ...
- 【LeetCode每天一题】Pascal's Triangle(杨辉三角)
Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's t ...
- script命令录屏
关于linux上的操作,我们的确可以使用'history'命令来显示出来操作记录,但是有些时候,我们不仅仅需要知道做了什么,还需要知道操作的时候,产生了什么效果,这个时候‘history’命令就显示无 ...
- Hybrid设计--核心交互
普通网页中跳转使用a标签,这里我们要对跳转进行更多的干预,所以将全站的跳转收口到框架层,用forward去实现.拒绝用a和window.location.如果我想对所有跳转做一个处理,开动画或者对跳转 ...
- Bug 5323844-IMPDP无法导入远程数据库同义词的同义词
参见MOS文档: Bug 5323844 - SYNONYM for a SYNONYM in remote database not imported using IMPDP (文档 ID 5323 ...
- HTML布局规范
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C# JArray与JObject 的使用
STEP1.using Newtonsoft.Json.Linq; STEP2 如何获取json里的某个属性(节点)值,对其删改,新增 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- CRUD简单查询
一.查询所有数据 select * from car 二.查询指定列 select code , price from car 三.修改查询出的列名 select code as '代号' , nam ...
- Vue系列之 => 通过vue-resource发起ajax请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jQuery属性--addClass()和removeClass()
addClass(class|fn) 概述 为每个匹配的元素添加指定的类名 参数 class 一个或多个要添加到元素中的CSS类名,请用空格分开: function(index, class) ...