您的位置:首页 > Web前端 > JavaScript

JS常用基础知识

2012-05-16 16:02 225 查看

1.1 this用法

最近的函数直接是某个对象的成员,那么this的值就是这个对象。如果包含this的最近的函数不是某个对象的成员,那么this的值便设为某种全局对象(常见为浏览器中的window对象)。

var obj = {

toString:function(){

return "obj的范围内";

},

func:function(){

alert(this);//这里的函数直接从属于对象obj

var innerFunc = function(){

//这里的函数不是特定对象的直接成员,只是另一个函数的变量而已。

alert(this);

};

innerFunc();

}

};

obj.func();

结果:以此输出“obj的范围内”和“Object Window”

1.2 JS原始数据类型

Undefined数据类型的值只有一个:undefined。

Null数据类型的值只有一个:null。

Boolean数据类型的值有两个:true和false。

Number

String

1.3 arguments的使用

每一个函数对象都有一个length属性,表示该函数期望接收的参数格式;

arguments.length表示函数实际接收的参数格式

eg:

function test(a,b,c){

alert(length); //结果:0

alert(arguments.length); // 结果:1

}

test("");



1.4 NOT AND OR

NOT : 对象 返回false

数字0 返回true

非0以外的数字 返回false



AND:一个运算数是对象,一个布尔 返回对象

二个运算数都是对象 返回第二个对象



OR:一个运算数是对象,一个布尔 返回对象

二个运算数都是对象 返回第一个对象

1.5 == 和 ===

== 进行类型转换后比较

=== 检测相等不执行类型转换

1.6 typeof和instanceof

typeof是一元运算符,后跟变量的名称,用于获取变量的数据类型,其返回值有5个:undefined、boolean、number、string以及object。

eg:

typeof(1):number

typeof(NaN): number

typeof(Number.MIN_VALUE): number

typeof(Infinity): number

typeof("123"): string

typeof(true): boolean

typeof(window): object

typeof(Array()): object

typeof(function(){}): function

typeof(document): object

typeof(null): object

typeof(eval): function

typeof(Date): function

typeof(sss): undefined

typeof(undefined): undefined



instanceof

instanceof 判断一个变量是否某个对象的实例.

eg:

[] instanceof Array; //true

[] instanceof Object; //true

new Person() instanceof Person; //true

new Person() instanceof Object; //true

new Person() instanceof Date; //false

document.getElementById('log')instanceof HTMLDivElement;//true

windowinstanceof Object //false

1.7 undefined

如果函数没有声明返回值,那么会返回undefined。

undefined实际上是从null派生出来的。

eg:

undefined==null //true

undefined===null //false 检测了类型

1.8 自定义事件

创建事件

var event = document.createEvent(type);

初始化

event.initEvent(type,bubbles,cancelable);

type:自定义事件名称

bubbles:是否冒泡

cancelable:是否取消

监听事件

target.addEventListener(type,Listener,useCapture);

Listener:触发事件时执行的函数

派发事件

element.disPatchEvent(event);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: