ECMAScript 中所有函数的参数都是按值传递。也就是说,把函数外部的值复制给函数内部的参数,就和把值从一个变量复制到另一个变量一样 ——《JavaScript高级程序设计(第三版)》
本篇博客将围绕书中这一知识点进行深入分析。
首先要弄清楚一些概念:变量、值、值的类型、引用、对象。如果这些概念没搞清楚,就没办法讨论这个问题。
- 变量:变量就是一个定义在某个作用域的名字(name),它是一个identifier。它绑定(bind)了一块内存。
- 值:值就是变量绑定的那块内存里面存放的数据。至于存放的数据的具体代表什么(含义是什么),暂且不管。但是本质上就是一段二进制数。
- 值的类型:即变量绑定的那块内存里放的数据应该如何去解释,或者说如何解码。在JS中值的类型有很多种,但是可以归结成两类:基本类型和引用类型。基本类型就是说:变量绑定的那块内存里放的数据的含义就是它看起来的那个样子。引用类型就是说:变量绑定的那块内存里放的数据,并不是我真正想要的数据,它是一个元数据、一个地址,一个指向我真正想用的数据的位置的指针。
- 引用:指向对象的指针
- 对象:上面说的那些不能被变量直接绑定,而是要通过引用来间接绑定的数据就是对象。而且对象数据与非变量数据相比有一个特点,即它是有内部结构的,即属性。
ECMAScript 变量包含两种不同的数据类型的值:基本类型值和引用类型值,其中基本类型值是按值访问的,引用类型值按引用访问。
let num1 = 5;
let num2 = num1;
复制后变量对象: | 变量 | 值 | | :—: | :————: | | num1 | 5 (Number类型) | | num2 | 5 (Number类型) |
复制后,num1与num2中的值相互独立,不会相互影响。
var a = new Object();
var b = a;
a.name = 'Alice';
console.log(b.name); // Alice
复制后变量对象: | 变量 | 值 | | :—: | :——————–: | | a | reference (Object类型) | | b | reference (Object类型) |
复制引用类型时,同样也是阿静存储在变量对象中的值复制一份放到新变量的空间,不同的是,复制的值实际上是一个指针,会指向同一个对象。,所在上述代码中对b对应的对象进行改变,a对应的对象也被影响到。
弄清楚了上面的概念,我们再来理解ECMAScript中所有函数的参数都是按值传递的
,还是看原书的示例代码
function addTen(num) {
num += 10;
return num;
}
var count = 20
var result = addTen(count);
alert(count); //20
alert(result); //10
可以看到,在基本类型中,count并没有因为addTen(count)而发送改变,证明参数确实是按值传递的,如果参数按引用传递,那么count也会跟着+10,变为30
function setName(obj) {
obj.name = "Nicholas";
obj = new Object();
obj.name = "Alice"
}
var person = new Object();
setName(person);
alert(person.name); //"Nicholas"
以上代码可以证明传递引用类型参数时也是按值传递的,因为在setName方法中,及时给obj赋值一个新对象但并没有改变入参person的引用。
按值传递。
// TODO
// TODO
本篇博客主要记录JavaScript的数据类型相关的知识。
查阅最新的ECMAScript 2020可知,JavaScript一共有7种基本数据类型(primitives),Undefined、Null、Boolean、Number、BigInt(新加类型)、String、Symbol,以及Object类型
我们从运行时的角度去看 JavaScript 的类型系统。运行时类型是代码实际执行过程中我们用到的类型。所有的类型数据都会属于8个类型之一。从变量、参数、返回值到表达式中间结果,任何 JavaScript 代码运行过程中产生的数据,都具有运行时类型。
可以通过typeof
操作符进行类型的确认
undefined --- 值为定义
boolean --- 值为布尔值
string --- 值为字符串
number --- 值为数值
object --- 值为对象或null
function --- 值为函数
调用typeof null返回object,是因为特殊值null被认为是一个空的对象
The value null is written with a literal: null. null is not an identifier for a property of the global object, like undefined can be. Instead, null expresses a lack of identification, indicating that a variable points to no object. In APIs, null is often retrieved in a place where an object can be expected but no object is relevant.
ECMAScript在第三版中引入undefined
,追要为了正式区分空对象指针(null)与未经初始化的变量(undefined)
// foo does not exist. It is not defined and has never been initialized:
foo; //ReferenceError: foo is not defined
//typeof foo --- undefined
// foo is known to exist now but it has no type or value:
var foo = null;
foo; //null
//typeof foo --- object
对null和undefined进行==
、===
的测试,可以看到,实际上undefined值是派生自null值的,因此null == undefined返回true。
typeof null // "object" (not "null" for legacy reasons)
typeof undefined // "undefined"
null === undefined // false
null == undefined // true
null === null // true
null == null // true
!null // true
isNaN(1 + null) // false
isNaN(1 + undefined) // true
如果定义的变量准备在将来保存对象,可以将变量初始化为null,这样只要直接检查null值,就可以进行对象的操作,体现null作为空指针的惯例,也可以区分undefined
if (car != null) {
// code
}
/*You can use undefined and the strict equality and inequality operators to determine whether a variable has a value.
*/
// first alternative
var x;
if (x === undefined) {
// these statements execute
}
else {
// these statements do not execute
}
// x has not been declared before
if (typeof x === 'undefined') { // evaluates to true without errors
// these statements execute
}
if (x === undefined) { // throws a ReferenceError
}
// second alternative
if ('x' in window) {
// these statements execute only if x is defined globally
}
// third alternative
var x;
if (x === void 0) {
// these statements execute
}
// y has not been declared before
if (y === void 0) {
// throws a - Uncaught ReferenceError: y is not defined
}
This is exactly what bash allows you to do. It has editing modes that allow you to edit command lines with editing commands similar to those of the two most popular UNIX editors, vi
and emacs
. It also provides a much-extended analog to the C shell history mechanism called fc
(for fix command) that, among other things, allows you to use your favorite editor directly for editing your command lines. To round things out, bash also provides the original C shell history mechanism.
bash initially starts interactively with emacs-mode as the default, First, you can use the set command:
$ set -o emacs
or:
$ set -o vi
The second way of selecting the editing mode is to set a readline variable in the file .inputrc
.
Editing commands in vi input mode
Command | Description |
---|---|
DEL | Delete previous character |
CTRL-W | Erase previous word (i.e., erase until a blank) |
CTRL-V | Quote the next character |
ESC | Enter control mode (see below) |
Basic vi control mode commands |Command|Description| |:–:|:–:| |h|Move left one character| |l|Move right one character| |w|Move right one word| |b|Move left one word| |W|Move to beginning of next non-blank word| |B|Move to beginning of preceding non-blank word| |e|Move to end of current word| |E|Move to end of current non-blank word| |0|Move to beginning of line| |^|Move to first non-blank character in line| |$|Move to end of line|
Commands for entering vi input mode |Command|Description| |:–:|:–:| |i|Text inserted before current character (insert)| |a|Text inserted after current character (append)| |I|Text inserted at beginning of line| |A|Text inserted at end of line| |R|Text overwrites existing text|
Abbreviations for vi-mode delete commands |Command|Description| |:–:|:–:| |D|Equivalent to d$ (delete to end of line)| |dd|Equivalent to 0d$ (delete entire line)| |C|Equivalent to c$ (delete to end of line, enter input mode)| |cc|Equivalent to 0c$ (delete entire line, enter input mode)| |X|Equivalent to dl (delete character backwards)| |x|Equivalent to dh (delete character forwards)|
vi control mode commands for searching the command history |Command|Description| |:–:|:–:| |k or -|Move backward one line| |j or +|Move forward one line| |G|Move to line given by repeat count| |/string|Search backward for string| |?string|Search forward for string| |n|Repeat search in same direction as previous| |N|Repeat search in opposite direction of previous|
** Miscellaneous vi-mode commands** |Command|Description| |:–:|:–:| |~|Invert (twiddle) case of current character(s)| |-|Append last word of previous command, enter input mode| |CTRL-L|Clear the screen and redraw the current line on it; good for when your screen becomes garbled| |#|Prepend # (comment character) to the line and send it to the history list; useful for saving a command to be executed later without having to retype it|
The default startup file is called .inputrc
and must exist in your home directory if you wish to customize readline. You can change the default filename by setting the environment variable INPUTRC
A shell is any user interface to the UNIX operating system, i.e., any program that takes input from the user, translates it into instructions that the operating system can understand, and conveys the operating system’s output back to the user.
There are various types of user interfaces. bash belongs to the most common category, known as character-based user interfaces
.
Remember that the shell itself is not UNIX just the user interface to it. UNIX is one of the first operating systems to make the user interface independent of the operating system.
The first major shell was the Bourne shell (named after its inventor, Steven Bourne); it was included in the first popular version of UNIX, Version 7, starting in 1979.
The first widely used alternative shell was the C shell, or csh. This was written by Bill Joy at the University of California at Berkeley as part of the Berkeley Software Distribution (BSD) version of UNIX that came out a couple of years after Version 7.
In recent years a number of other shells have become popular. The most notable of these is the Korn shell. This shell is a commercial product that incorporates the best features of the Bourne and C shells, plus many features of its own.
The Bourne Again shell (named in punning tribute to Steve Bourne’s shell) was created for use in the GNU project.[2] The GNU project was started by Richard Stallman of the Free Software Foundation (FSF) for the purpose of creating a UNIX-compatible operating system and replacing all of the commercial UNIX utilities with freely distributable ones.
bash, intended to be the standard shell for the GNU system, was officially “born” on Sunday, January 10, 1988. Brian Fox wrote the original versions of bash and readline and continued to improve the shell up until 1993.
You might be able to find the location by typing whereis bash
(especially if you are using the C shell); if that doesn’t work, try whence bash
, which bash
, or this complex command
Files are the most important types of “things” on any UNIX system. A file can contain any kind of information, and indeed there are different types of files. Three types are by far the most important:
cd -
: changes to whatever directory you were in before the current one.
ls -l
’: tells ls to list the file’s owner, size, time of last modification, and other information.
ls -a
: lists all files including the hidden files.
Basic wildcards
|Wildcard|Matches|
|:–:|:–:|
|?|Any single character|
|*|Any strings characters|
|[set]|Any character in set|
|[!set]|Any character not in set|
pathname expansion: it ispossible to use wildcards in the current directory, they can also be used as part of a pathname.
Brace Expansion: echo b{ed,olt,ar}s
echo {d..h}
h.
The UNIX I/O scheme is based on two dazzlingly simple ideas.
Popular UNIX data filtering utilities |Utility|Purpose| |:–:|:–:| |cat|Copy input to output| |grep|Search for strings in the input| |sort|Sort lines in the input| |cut|Extract columns from input| |sed|Perform editing operations on input| |tr|Translate characters in the input to other characters| all of them (and most other UNIX utilities) accept input from standard input if you omit the argument.
nice
command, where command can be a complex shell command line with pipes, redirectors, etc., then the command will run at a lower priority.
leetcode 344. Reverse String
Write a function that reverses a string. The input string is given as an array of characters char[].
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Input: [“h”,”e”,”l”,”l”,”o”] Output: [“o”,”l”,”l”,”e”,”h”]
Input: [“H”,”a”,”n”,”n”,”a”,”h”] Output: [“h”,”a”,”n”,”n”,”a”,”H”]
class Solution {
public void reverseString(char[] s) {
if (s == null || s.length == 0) {
return;
}
for (int i = 0; i < s.length / 2; i++) {
char temp = s[i];
s[i] = s[s.length - i - 1];
s[s.length - i - 1] = temp;
}
}
}
Runtime: 1 ms, faster than 100.00% of Java online submissions for Reverse String. Memory Usage: 47 MB, less than 90.49% of Java online submissions for Reverse String.
Follow these steps to solve any Dynamic Programming interview problem
leetcode 242. Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s.
Input: s = “anagram”, t = “nagaram” Output: true Input: s = “rat”, t = “car” Output: false
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1, str2);
}
}
Time complexity : O(nlogn). Assume that nn is the length of ss, sorting costs O(nlogn) and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(nlogn).
Space complexity : O(1). Space depends on the sorting implementation which, usually, costs O(1) auxiliary space if heapsort is used. Note that in Java, toCharArray() makes a copy of the string so it costs O(n) extra space, but we ignore this for complexity analysis.
Runtime: 7 ms, faster than 32.76% of Java online submissions Memory Usage: 37.3 MB, less than 76.69% of Java online submissions
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] counter = new int[26];
for (int i = 0; i < s.length(); i++) {
counter[s.charAt(i) - 'a']++;
counter[t.charAt(i) - 'a']--;
}
for (int count : counter) {
if (count != 0) {
return false;
}
}
return true;
}
}
Runtime: 4 ms, faster than 77.25% of Java online submissions for Valid Anagram. Memory Usage: 36.3 MB, less than 81.82% of Java online submissions for Valid Anagram.
The notes for requests – python library The parameters are URL、Method、Headers、Body and so on.
This week I watched a video about Test Driven Development with Spring Boot, the video is friendly to new development.