JavaScript in the Browser
以下内容出自《JavaScript高级程序设计》第五章 JavaScript in the Browser
关于document.write
document.write("<script type='text/javascript' src='vapour.js'>" + "</scr" + "ipt>");
This code writes a <script/> tag to the page, which causes the browser to load the external JavaScript file as it would normally. Note that the string "</script>" is split into two parts ("</scr" and "ipt>". This is necessary because anytime the browser sees </script>, it assumes that the code block is complete (even if it occurs inside of a JavaScript string).
write和writeln方法必须在页面加载完成之前调用,否则会清空页面内容
Remember that both write() and writeln() must be called before the page has been fully loaded in order to insert the content properly. If either method is called after the page is loaded, it erases the page and displays the content specified.
Object Basics
下面内容出自《JavaScript高级程序设计》第三章 Object Basics:
this总是指向调用它的对象
One of the most important concepts to grasp in ECMAScript is the use of the this keyword, which is
used in object methods. The this keyword always points to the object that is calling a particular
method。
字符串类型的值是不可改变的
Similar to other languages, ECMAScript strings are immutable, meaning that their value cannot be changed.
考虑如下代码:
var str = "hello ";
str += "world";
以上代码按以下步骤执行:
- 创建一个字符串类型存储 "hello "
- 创建一个字符串类型存储 "world"
- 创建一个字符串类型存储结果
- 复制str的内容到存储结果
- 复制 "world" 到存储结果
- 将str指向存储结果
可以看出,如果以上步骤执行很多次,将会带来性能灾难。连接多个字符串推荐使用数组存储,最后使用join方法连接。
Object对象
ECMAScript中的所有原生对象都继承自Object对象,所以任何对Object对象的修改都会影响到其它原生对象。看以下代码:
Object.prototype.showValue = function () {
alert(this.valueOf());
};
var str = 'hello';
var iNum = 25;
str.showValue(); //outputs 'hello'
iNum.showValue(); //outputs '25'
ECMAScript Basics
最近在读《JavaScript高级程序设计》,发现了自己的很多概念和理解上的误区,特此记录。下面信息出自第二章 ECMAScript Basics
ECMAScript解释器会将未声明而直接使用的变量声明为全局变量
When the ECMAScript interpreter sees an identifier that hasn’t been declared, it creates a global variable with the given name of the identifier and initializes it with the value specified.
关于typeof(null)返回object的原因:将错就错。
You may wonder why the typeof operator returns “object” for a value that is null. This was actually an error in the original JavaScript implementation that was then copied in ECMAScript. Today, it is rationalized that null is considered a placeholder for an object, even though, technically, it is a primitive value.
数学运算中的返回结果都是十进制
Even though integers can be represented as octal and hexadecimal literals, all mathematical operations return decimal results.
利用toString()方法实现进制转换,默认转换成十进制
In default mode, the Number’s toString() method always returns the decimal representation of the number, regardless of how you originally specified it. Therefore, numbers specified by octal or hexadecimal literals are output as decimal.
parseFloat()只接收十进制形式参数,忽略其它进制(二进制、八进制 、十六进制)
parseFloat() method ignores leading zeros, so the octal number 0908 will be parsed into 908, and the hexadecimal number 0xA will return NaN because x isn’t a valid character for a floating-point number. There is also no radix mode for parseFloat().
字符串操作中的slice()方法和substring()方法的区别:对负数的处理方式不同。
For the slice() method, a negative argument is treated as the length of the string plus the negative argument; the substring() method treats a negative argument as 0 (which means that it is ignored).
逻辑与运算符并不是返回布尔值:
- If one operand is an object and one is a Boolean, the object is returned.
- If both operands are objects, the second operand is returned.
- If either operand is null, null is returned.
- If either operand is NaN, NaN is returned.
- If either operand is undefined, an error occurs.
逻辑与、逻辑或运算符,如果第一个操作数能确定结果,则直接返回,不计算第二个操作数。
当一个函数没有明确返回值,或使用return,但无返回值。函数将返回undefined作为返回值。
When a function doesn’t explicitly return a value or uses the return statement without a value, the function actually returns undefined as its value.
闭包的定义:函数中包含函数外部的变量
Closures are functions whose lexical representation includes variables that aren’t evaluated, meaning that functions are capable of using variables defined outside of the function itself.