JavaScript深入

来源:互联网 发布:淘宝微信内部优惠卷群 编辑:程序博客网 时间:2024/06/05 02:49

内容来自于JavaScript深入浅出

JavaScript深入浅出一

数据类型

JavaScript有5种原始类型:

  • number
  • string
  • boolean
  • null
  • undefined

还有一种object对象类型,在Javascript中FunctionArrayDate都是对象类型。

隐式转换

字符串和数字相加+,结果转换为字符串

var x = 'The answer is ' + 42;

而字符串和数字相减-,结果转发为数字

"37"-7 //结果为30"37"+7//结果为"377"

所以,可以巧用+/-规则转换类型。

==比较

"1.23" == 1.23//true0 == false//truenull == undefined//truenew Object() == new Object()//false[1,2] == [1,2]//false

object==number|string尝试对象转为基本类型 new String('hi') == 'hi'true,其它false。

规则如下In JavaScript, why is “0” equal to false, but when tested by ‘if’ it is not false by itself?:

Equal (==)

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

nullundefined,来自与undefined与null的区别

null表示”没有对象”,即该处不应该有值。典型用法是:

  • 作为函数的参数,表示该函数的参数不是对象。
  • 作为对象原型链的终点。

    Object.getPrototypeOf(Object.prototype)// null

undefined表示”缺少值”,就是此处应该有一个值,但是还没有定义。典型用法是:

  • 变量被声明了,但没有赋值时,就等于undefined。

  • 调用函数时,应该提供的参数没有提供,该参数等于undefined。

  • 对象没有赋值的属性,该属性的值为undefined。

  • 函数没有返回值时,默认返回undefined。

var i;i // undefinedfunction f(x){console.log(x)}f() // undefinedvar  o = new Object();o.p // undefinedvar x = f();x // undefined

严格等于===

  • 类型不同,返回false
  • 类型相同

    null === null//trueundefined === undefined//trueNaN === NaN//falsenew Object() === new Object()//false

包装对象

JavaScript是面向对象的语言,使用”.”操作符可以访问对象的属性和方法,而对于基本类型(null, undefined, bool, number, string)应该是值类型,没有属性和方法,然而:

var str = 'String;str.length;//6str.t = 10;str.t//undefined

为什么会这样呢?

在Javascript中当把一个基本类型当做对象去使用时,例如访问其属性,Javascript会自动把基本类型转换为包装类型对象,想当于new了一个临时的对象,当完成访问后,临时对象会被销毁掉。

类型检测

typeof运算符

typeof 返回 typeof 100 ‘number’ typeof true ‘boolean’ typeof function ‘function’ typeof(undefined) ‘undefined’ typeof new Object() ‘object’ typeof [1,2] ‘object’ typeof NaN ‘number’ typeof null ‘object’

instanceof是基于原型链的判读,常用对象的判断 obj instanceOf Object

function Person(){}undefinedfunction Student(){}undefinedStudent.prototype = new Person()Person {}Student.prototype.constructor = StudentStudent(){}var bosn = new Student()undefinedbosn instanceof Studenttruevar one = new Person()undefinedone instanceof Studentfalse

要注意的是:不同window或iframe间的对象类型检测不能使用instanceof

使用Object.prototype.toString

Object.prototype.toString.apply([]) === "[object Array]"Object.prototype.toString.apply(function(){}) === "[object Function]"Object.prototype.toString.apply(null) === "[object Null]"Object.prototype.toString.apply(undefined) === "[object Undefined]"

注意:IE6/7/8Object.prototype.toString.apply(null)返回”[object Object]”

类型检测小结

方式 说明 typeof 适合基本类型及function检测,遇到null失效 [[Class]] 通过{}.toString拿到,适合内置对象和基元类型,遇到null和undefined失效(IE678等返回[object Object]) instanceOf 适合自定义对象,也可以用来检测原生对象,在不同iframe和window间检测时失效

表达式和运算符

var val = (1,2,3);//3

delete运算符删除对象上的属性
in运算符

window.x = 1;'x' in window;//true

语句

块block

注意:没有块级作用域
所以如下的两种形式是等价的:

块block

在循环之后还可以拿到i

在函数里面定义的变量,在函数的外面是拿不到的,相当于是函数作用域:

function foo(){    var a=1;    console.log(a);//1}foo();console.log(typeof a);//'undefined'

如下,var a=b=1;,a是个函数里的局部的变量,但b却是个全局的变量。

function foo(){    var a=b=1;}foo();console.log(typeof a);//'undefined'console.log(typeof b);//'number'

可以这样写来避免var a=1,b=1;

对象

创建对象的方式

1.对象字面量
2.使用new构造器

function foo() {}foo.prototype.z = 3;var obj = new foo();obj.y = 2;obj.x = 1;typeof obj.toString;//'function''z' in obj//trueobj.hasOwnProperty('z');//false

3.Object.create创建对象

var obj = Object.create({x:1});obj.x//1typeof obj.toString;//'function'obj.hasOwnProperty('x');//falsevar obj = Object.create(null);obj.toString;//undefined
0 0