转 javascript

1. 各java一样采用Unicode编码.

2. 可省略分号, 如

return

true

<=>

return;

true;

而非return true;

这个感觉不爽, 容易出错而不知.

3. 大小写敏感.

数据类型:

4. null, 空值, 表示的是无值; undefined, 未定义, 表示未赋值或不存在对象属性.

对于==来说两者并无区别, ===和typeof区分两者.

5. 数组和对象都是数值的集合, 对象中的每个数值都有一个名字, 而数组中的每个数值都有一个数字, 即下标.

基本数据类型的包装对象Number, Bealean, String, 和java真像. 必要时自动转换, 所以两者使用并无多大差别.

5. 隐式声明的变量总是被创建为全局变量, 不管在哪里.

6. 没有块级作用域

函数中声明的所有变量, 在整个函数中有定义, 但在初始化语句之前并没被初始化. 如:

var scope = “global”;

function f()

{

alert(scope); // 显示 “undefined”

var scope = “local”; // 初始化, 但整个函数中有定义

alert(scope); // 显示 “local”

}

f();

7. 基本数据类型: 数值, 布尔值, null, undefined

引用类型: 对象, 数组, 函数

字符串: 复制对字符串的引用, 其他表现又与基本类型相似, 比较时使用的是值, 这点与Java不同. 值不可变.

8. Javascript解释器开始运行时, 首先创建一个全局对象, 而声明的全局变量实际上是它的属性. 局部变量是调用对象(call object)的属性. call object生命期较短.

所以变量和对象的属性实质上是一样的, 这样随意的添加属性就和定义变量一样, 没啥奇怪的了.

每个框架或窗口都有各自的执行环境, 各自全局对象, 但可以互相引用.

9. in运算符:

判断是否左边是右边的属性名.

var point = {x:1, y:1};

var a = “x” in point; // true

var b = “tostring” in point; // true, 继承属性

10. delete

var o = {x:1, y:1};

delete o.x;

删除属性, 变量, 数组元素.

注意的是当delete的不是属性, 数组元素或变量, 也返回true.

var声明的变量不能删除, 某些核心属性和客户端属性也不能.

v1.1前只是设为null.

11. void运算符

舍弃运算数的值, 返回undefined.

<a href=”javascript:void window.open();” >Open New Window</a>

这个的作用是由于open()返回值被转为undefined, 所以仅仅弹出一个窗口, 而原页面并不跳转, 如果改为如下, 而页面将跳转到空白页面.

<a href=”window.open();” >Open New Window</a>

12. .运算符左边必须是一个标志符.

13. for/in

for (variable in object)

statement

14. with

暂时修改作用链

with (object)

statement


对象


15. 对象创建:

var o = new Object();

o.x = 1.1;

o.xx = true;

var now = new Date();

var pattern = new RegExp(“\\sjava\\s”, “i”);

可以以关联数组的方式引用, 实际上数据也是一种特殊的对象.

o[“x”], o[“xx”]

16. 对象直接量

var point = {x:2.3, y:-1.2};

17. 嵌套

var retangle = {upperLeft:{x:2, y:2}, lowRight:{x:4, y:4}};



函数

18. 函数是一个真正的数据类型, 可以被存储在变量, 数组和对象中, 可以作为参数. 函数实际上为一个特殊的对象.

19. 函数在定义它的作用域中执行.

// This function returns a function each time it is called // The scope in which the function is defined differs for each call

function makefunc(x) {

return function( ) { return x; }

}

// Call makefunc( ) several times, and save the results in an array:

var a = [makefunc(0), makefunc(1), makefunc(2)];

// Now call these functions and display their values.

// Although the body of each function is the same, the scope is

// different, and each call returns a different value:

alert(a[0]( )); // Displays 0

alert(a[1]( )); // Displays 1

alert(a[2]( )); // Displays 2

19. 函数定义

1).

function square(x)

{

return x*x;

}

2).

函数直接量定义

function [函数名可选](参数){函数体}

var square = function(x) {return x*x;}

3).

lambda

var square = new Function(“x”, “return x*x;”); // x是参数, return x*x;是函数体.

20.对象构造函数:

// 无返回值, 否则new的结果就是返回值, 而不是该对象了.

function Rectangle(w, h)

{

this.width = w;

this.height = h;

}

var rect = new Rectangle(2, 4);


21. 属性的继承只发生在读属性值时, 写属性不会发生. 因为JS中函数(方法)都是数据类型, 所以把这些方法和常量放在原型(prototype)中, 使这部分空间被多个对象共享, 这样便可以大大节省内存.

New Circle(); // 先new是为了兼容JS1.1. 大多数版本中, 每个函数都自动有一个空的原型对象, 而JS1.1只有在该函数首次被用作构造函数时, 才会创建一个原型.

Rectangle.prototype.pi = 3.14159;

function cal_area() { return this.w * this.h; }

Rectangle.prototype.calAera = cal_area;



22. 因为函数也是对象, 所以类属性就是函数属性, 是构造函数的属性.

类方法


23. 函数实际参数:

arguments对象, 可下标, 属性有length, callee.

function f(x, y, z)

{

if (arguments.length > 3)

{

alert(arguments[3]);

}

arguments[0] = 1;

alert(x); // 显示1, 因为x与arguments[0]引用同一变量

}

function (x)

{

if (x <= 1)

{

return 1;

}

return x * arguments.callee(x-1):

}


24. Function.length, 函数定义中的参数个数

function check(args)

{

if (args.length != args.callee.length) // 实际个数 != 定义个数

{

throw new Error(“Haha”);

}

}

function f(x, y, z)

{

check(arguments);

}

25. 自定义函数属性, 可实现像java, c++中的函数静态变量.

f.counter = 0;

function f()

{

return f.counter++;

}

26. 每个函数都有apply()和call()

f.call(ob, 1, 2);

<=>

ob.m = f;

ob.m(1, 2);

delete ob.m;

<=>

f.apply(ob, [1, 2]); // apply区别是参数作为数组元数传进.

var biggest = Math.max.apply(null, [1, 2, 3, 4]);

Javascript1.2实现了apply()方法, Javascript1.5实现了call()方法.

27. Function()

1).

允许在运行时动态地创建和编译Javascript代码. 而函数直接量是程序结构的静态部分, 就和function语句一样.

2).

每次调用Function()时都会解析函数体并且创建一个新的函数对象. 这在循环中效率会很低, 而函数直接量和嵌套函数都只编译一次.

3).

使用构造函数Function()创建的函数不使用词法作用域, 它们总是被当作顶级函数来编译.

var y = “global”;

function constructFunction()

{

var y = “local”;

return new Function(“return y”); // 不捕捉局部作用域

}

alert(constructFunction()()); // 显示 “global”

28. Javascript中的继承

以原型(prototype)为基础的继承

Function Hello(a)

{

this.x = a;

}


Function MyHello(b)

{

this.y = b;

}


MyHello.prototype = new Hello(0);

MyHello.prototype.constructor = MyHello; // Javascript1.1中这个属性只读, 不能这样定义

这样, 在MyHello中查询属性时, 首先查询的是这个对象本身, 其次是MyHello.prototype, 再其次是Hello.prototype, 最后是Object.prototype. 构造函数的原型默认就是Object, 除非重新定义.



29. 关联数组的好处

object.property ? object[“property”]

前者属性是标志符, 后者是字符串, 后者就能实现灵活动态的效果, 如果反射.

var addr = “”;

for (i=0; i<4; i++)

{

addr += customer[“address”+i] + ‘\n’;

}


var value = 0;

for (stock in portfolio)

{

Value += portfolio[stock] * getValues(stock);

}

30. for/in循环遍历所有对象的所有可能的属性. 实际上for/in有不能枚举出来的属性, 如一些标记成了只读的, 永久的或者不可枚举的属性, e.g. Object.valueof

for/in可以用于数组.



31. Object的属性

所有对象继承Object.

32. constructor属性

if ((typeof o == “Object”) && (o.construcotr == Date)) // 判断o是否为Date.


33. toString(), toLocaleString() 和 valueof()

默认的toString()只提供”[object Object]”信息. 有时想要调用这个函数以实现”type of “的功能, 这样必须明确指明调用它:

Object.prototype.toString.apply(o);

如下:

function Typeof(x)

{

var t = typeof x;

if (t != “Object”)

{

return t;

}

var c = O bject.prototype.toString.apply(x);

c = c.substring(8, c.length-1); //去掉”[object” 和 “]”

return c;

}


默认toLocaleString()与toString()一样, 不过很多类会定义它, 包括内部类, e.g. Array, Date, Number, etc.


默认valueof()不进行任何转换. 注意有的环境valueof()的优先级比toString()高, 所以有时最好还是强制调用valueof().


34. hasOwnProperty() 判断是否是非继承的属性, 不是和无这个属性都返回false.


35. isPrototypeOf() 判断调用对象是否为参数的原型对象, 从函数名就知.



数组

36. 数组创建:

1).

var a = new Array();

a[0] = 1.2;

a[1] = “java”;

a[2] = true;

a[3] = {x:1, y:3};

2).

var a = new Array(1.2, “java”, true);

3).

var a = new Array(10); //10个未定义元素

4).

数组直接量

var a = [1.2, “Java”, true];

var matrx = [[1, 2, 3], [2, 3, 4], [5, 6, 7]];

var base = 100;

var table = [base, base+1, base+2];

var sparseArray = [1,,,,100]; // 其中有三个未定义元素

37. 数组下标为0—2^32-1的整数, 其他值将被转换成字符串看待, 即关联数组看待.

38. JS中数组是稀疏的(sparse), 这样如下只需为两个元素分配内存:

a[0] = 1;

a[10000] = “haha”;


var c = new Circle(1, 2, 3);

c[0] = “hello”;

上面只是定义了一个名为”0”的对象属性, 而非数组, 数组必须用Array()或数组直接量定义, 尽管数组是一个特殊的对象. 不过数组有一些特性.

39. 所有数组含属性length. 像上上面那样, a.length的值为10001, 而非2, 虽然只分配了两个元素的内存.


40. JS中无多维数组, 和JAVA一样, 可以嵌套. 但可以使用多维数组的表达多式, 即多个中括号.


41. 数组有很多方法:

join(), reverse(), sort(), concat(), slice(), splice(); push(), pop(); unshift(), shift(), toString(), toSource().



Javascript正则表达式

42. 是perl5正则表达式的子集.

var pattern = /s$/; // 正则表达式直接量

var pattern2 = new RegExp(“s$”);

43. 特殊字符

^ $ . * + ? = ! : | \ / ( ) [ ] { }

44. 语法

Alphanumeric character

Itself

\0

The NUL character (\u0000)

\t

Tab (\u0009)

\n

Newline (\u000A)

\v

Vertical tab (\u000B)

\f

Form feed (\u000C)

\r

Carriage return (\u000D)

\xnn

The Latin character specified by the hexadecimal number nn; for example, \x0A is the same as \n

\uxxxx

The Unicode character specified by the hexadecimal number xxxx; for example, \u0009 is the same as \t

\cX

The control character ^X; for example, \cJ is equivalent to the newline character \n

[...]

Any one character between the brackets.

[^...]

Any one character not between the brackets.

.

Any character except newline or another Unicode line terminator.

\w

Any ASCII word character. Equivalent to [a-zA-Z0-9_].

\W

Any character that is not an ASCII word character. Equivalent to [^a-zA-Z0-9_].

\s

Any Unicode whitespace character.

\S

Any character that is not Unicode whitespace. Note that \w and \S are not the same thing.

\d

Any ASCII digit. Equivalent to [0-9].

\D

Any character other than an ASCII digit. Equivalent to [^0-9].

[\b]

A literal backspace (special case).

{n,m}

Match the previous item at least n times but no more than m times.

{n,}

Match the previous item n or more times.

{n}

Match exactly n occurrences of the previous item.

?

Match zero or one occurrences of the previous item. That is, the previous item is optional. Equivalent to {0,1}.

+

Match one or more occurrences of the previous item. Equivalent to {1,}.

*

Match zero or more occurrences of the previous item. Equivalent to {0,}.

|

Alternation. Match either the subexpressions to the left or the subexpression to the right.

(...)

Grouping. Group items into a single unit that can be used with *, +, ?, |, and so on. Also remember the characters that match this group for use with later references.

(?:...)

Grouping only. Group items into a single unit, but do not remember the characters that match this group.

\num

Match the same characters that were matched when group number n was first matched. Groups are subexpressions within (possibly nested) parentheses. Group numbers are assigned by counting left parentheses from left to right. Groups formed with (?: are not numbered.

^

Match the beginning of the string and, in multiline searches, the beginning of a line.

$

Match the end of the string and, in multiline searches, the end of a line.

\b

Match a word boundary. That is, match the position between a \w character and a \W character or between a \w character and the beginning or end of a string. (Note, however, that [\b] matches backspace.)

\B

Match a position that is not a word boundary.

(?=p)

A positive look-ahead assertion. Require that the following characters match the pattern p, but do not include those characters in the match.

(?!p)

A negative look-ahead assertion. Require that the following characters do not match the pattern p.


45. 匹配是贪婪的, 但可以用?实现非贪婪的重复, 只需在重复字符后加?即可.

??, +?, *?, {2, 4}?

对于字符串abcd12345:

/\d{2, 4}/ // 匹配2到4 个数字, 匹配结果为1234

/\d{2, 4}?/ //结果为12

对于字符串 aaab

/a*b/ //匹配aaab

/a*?b/ //也匹配aaab, 而非b, 因为正则表达式模式匹配是在寻找字符串中第一个可能匹配的位置, 如果还要找下一个匹配, 则会在上一个匹配结束的后一个字符开始继续匹配.

46. 选择, 分组和引用

选择项是从左到右考虑, 直到发现匹配项. 如果左边的选择项匹配, 就忽略右边的选择项, 即使它产生更好的匹配.

对于字符串ab,

/a|ab/ //只匹配a

47. 括号的作用: 把单独的项目组合成子表达式, 以便|, *, +, ?等使用, 如/java(script)?/; 另一作用是定义子模式, /['”][^'”]*\1/.

48. 对正则表达式中前一子表达式的引用所指的并不是那个子表达式的模式, 而是与那个模式相匹配的文本. 另外, \num中的num是左括号的位置, 如/(java(script)?)\s(hehe)/的\2是指(script).


49. 标志

i

Perform case-insensitive matching.

g

Perform a global match. That is, find all matches rather than stopping after the first match.

m

Multiline mode. ^ matches beginning of line or beginning of string, and $ matches end of line or end of string.


50. 自动类型转换

只要把非空对象用在布尔环境中, 它就会被转换成true.

new Boolean(false) // 内部值是false, 但该对象将被转换成true.

51. 在大多数情况下, Js会先尝试调用对象的valueof方法进行转换, 或无valueof, 则再调用tostring进行转换. 但有一种例外, 当运算符 “+” 作用于Date对象时, 首先调用tostring进行转换.

52. 强制转换

var x_as_string = x + “”;

var x_as_number = x – 0;

var x_as_boolean = !!x;

var string_value = String(number);


var n = 17;

binary_string = n.tostring(2); // 10001

octal_string = “0” + n.tostring(8); // 021

hex_string = “0x” + n.tostring(16); // 0x11

原文地址:https://www.cnblogs.com/zwei1121/p/744748.html