JavaScript深入
来源:互联网 发布:淘宝微信内部优惠卷群 编辑:程序博客网 时间:2024/06/05 02:49
内容来自于JavaScript深入浅出
JavaScript深入浅出一
数据类型
JavaScript有5种原始类型:
number
string
boolean
null
undefined
还有一种object
对象类型,在Javascript中Function
、Array
、Date
都是对象类型。
隐式转换
字符串和数字相加+
,结果转换为字符串
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.
null
与undefined
,来自与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
运算符
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]”
类型检测小结
表达式和运算符
var val = (1,2,3);//3
delete
运算符删除对象上的属性 in
运算符
window.x = 1;'x' in window;//true
语句
块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
- 深入JavaScript
- JavaScript深入
- 深入javascript
- 深入JavaScript
- Javascript 深入了解Javascript 基础知识
- 深入理解JavaScript函数
- 深入解JavaScript函数
- 深入学习javascript
- 深入理解javascript
- 深入理解 javascript
- JavaScript click事件深入
- JavaScript click事件深入
- 深入理解JavaScript系列
- 深入理解JavaScript系列
- 深入理解JavaScript系列
- 深入javascript闭包
- 深入学习javascript
- 深入了解JavaScript
- modulejs解读
- JVM性能调优监控工具hprof使用详解
- 非分区表转换成分区表
- C#超基础:进制转换
- 交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别
- JavaScript深入
- 写一个程序,交换两个数
- android 将手机号中间隐藏为星号(*)
- Linux 服务器模型
- SQLServer解决孤立用户问题
- Linux常用命令-文件处理命令
- weblogic发送https请求的证书错误的解决办法
- iOS项目添加VPN
- JNI完全指南(二)——类与异常