1、javascript 面向对象
无标题文档
//oDiv.onclick = function () {
// alert(this);
//};
window.onload = function () {
var arr = [12, 65, 87];
//this:当前的方法,属于谁
//arr.show = function () {
// alert(this);
//};
arr.sssss = function () {
alert('123');
};
arr.sssss();
}
我擦,代码竟然可以这么写
2、构造函数与原型
无标题文档
//构造函数
function Person(name, sex) {
this.name = name;
this.sex = sex;
}
//原型
Person.prototype.showName = function () {
alert(this.name);
};
Person.prototype.showSex = function () {
alert(this.sex);
};
var p = new Person('blue', '男');
p.showName();
p.showSex();
小注: 每个对象有差异的东东,可以放到构造函数中,通用的可以使用原型
3、原型的优先级
无标题文档
Array.prototype.a = 12;
var arr = [1, 2, 3];
alert(arr.a); //12
arr.a = 5;
alert(arr.a); //5
delete arr.a;
alert(arr.a); //12
4、[Javascript中this关键字详解]
(http://blog.csdn.net/jiankunking/article/details/50413767)
5、事件绑定IE方式
attachEvent(事件名称, 函数),绑定事件处理函数
detachEvent(事件名称, 函数),解除绑定
DOM方式
addEventListener(事件名称,函数, 捕获)
removeEventListener(事件名称, 函数, 捕获)
//1.谁
//2.事件
//3.函数
function AddEvent(obj, sEvent, fn)
{
//IE
if(obj.attachEvent)
{
obj.attachEvent('on'+sEvent, fn);
}
else
{
obj.addEventListener(sEvent, fn, false);
}
}
6、绑定和this
window.onload = function () {
var oBtn = document.getElementById('btn1');
/*oBtn.onclick=function ()
{
alert(this);
};*/
//IE 事件绑定 this->window
/*oBtn.attachEvent('onclick', function (){
alert(this==window);
});*/
//FF
oBtn.addEventListener('click', function () {
alert(this);
}, false);
};
7、匿名函数
匿名函数绑定事件无法解除绑定 与C #一样嘛
作者:jiankunking 出处:http://blog.csdn.net/jiankunking