专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Javascript教程 » javascript面向对象:javascript 基础整理总结(面向对象) »正文

javascript面向对象:javascript 基础整理总结(面向对象)

来源: 发布时间:星期六, 2009年2月14日 浏览:0次 评论:0
="t18">javascript 学习

javascript 大体上可分为3个区别部分组成: 核心(ECMAscript),文本对象(DOM),浏览器对象(BOM)

核心(ECMAscript): 关键字语句运算符对象

文本对象(DOM):DOM将把整个页面规划成由节点层级构成文档.
解析遵循 W3C html dom 标准:
W3C dom 参考特别关注 DOM Node 介绍说明

BOM 浏览器对象. cookie,弹出新浏览器浏览器设置大小


核心(ECMAscript)Global 内置对象;
思路方法: parseInt,isNan,encodeURI...等都为此对象思路方法
特别注意 eval;动态语言象征 比如:eval("alert('hi')"); 但这个思路方法很邪恶(安全方面)
文本对象(DOM)介绍说明:


<bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> </bookstore>











1. ECMAscript基础

$ 变量弱类型 ; 匈牙利类型标示 : var iOuouValue=100;


$ 结束行分号有无都可以; 但再 _disibledevent=> $ 关键字 ; 提别注意


"typeof" var test=1; alert(typeof testX); //output "und"

"NaN" - not a number -> isNan("blue"); //output "true" ->isNan("123"); //output "false"

$ 对象; var o = Object; var a = {}


这里特别介绍说明下 我们普通写 个 function 就是个 object

这 var a = {name:"刘凯毅"} 等同和 var a = function{this.name="刘凯毅"};

来个 {name:"test",pass:"123456",addr:"bj"} //这是什么 ?! json


当 var str = '{name:"test",pass:"123456",addr:"bj"}'

var objectBean = eval(str); //这里就是 对象 objectBea.name 使用了

域概念:

<SCRIPT type=text/javascript>
var sMessage = 'Hello';
function Something {
sColor = 'red';
sMessage = 'Hello World!';
}
Something;
alert(sMessage); //Hello World!
alert(sColor); //red
</SCRIPT> <SCRIPT type=text/javascript>
var sMessage = 'Hello';
function Something {
var sColor = 'red';
sMessage = 'Hello World!';
}
Something;
alert(sMessage); //Hello World!
alert(sColor); // 什么都没有
</SCRIPT>

<SCRIPT type=text/javascript>
var sMessage = 'Hello';
function Something {
var sColor = 'red';
var sMessage = 'Hello World!';
}
Something;
alert(sMessage); //Hello
alert(sColor); // 什么都没有
</SCRIPT>






为面向对象做基础:object prototype 类型对象应用 参考

// 最简单 继承
Object.prototype.inObj = 1;

function A
{
this.inA = 2;
}

A.prototype.inAProto = 3;

B.prototype = A; // Hook up A o B's prototype chain
B.prototype.constructor = B;
function B
{
this.inB = 4;
}

B.prototype.inBProto = 5;

x = B;
document.write(x.inObj + ', ' + x.inA + ', ' + x.inAProto + ', ' + x.inB + ', ' + x.inBProto);

//1, 2, 3, 4, 5
//增加点信心 http://www.json.org/json.js

Object.prototype.toJSONString = function (filter) { JSON.y(this, filter); }; 后我们就可以使用 bean.toJSONString不是吗?


$ arguments ;

function getFun{alert(arguments.length);} ;




getFun("xx") //output 1

getFun("xx",23) //output 2







$ 语句 ;特殊介绍说明下 for

for(var i=0i<iCount;i) 或 for( attr in object ) ;


如果无聊 你可以 for( sProp in window ){alert(sProp+"你丫点啊!");} //看看 javascript 反射















面向对象:

var bean = Bean;




1.工厂思路方法

function getAttr{

alert(this.attr)


}

function Bean(tattr){

var bean = Object;

bean.attr = tattr;

bean.getAttr = getAttr;

bean ;


}

根本就是山寨版 面向对象


2.构造

function Bean(tattr){

this.attr = tattr ;

bean.getAttr = function{

alert(this.attr);

}

}

其上 2 总 再Bean 对象创建时思路方法会 “重复生成”!




3.原型模式

function Bean{}

Bean.prototype.attr = "";

Bean.prototype.getAttr=function{alert(this.attr);}




解决 “重复生成” 问题但新问题 Bean.prototype.getArray = Array;




对象 bean1 和 bean2 都会共享 Array 空间(是我们不想看到)




4.混合 模型 :) 哈哈

function Bean{

this.attr= "";


this.getArray= Array;


}

Bean.prototype.getAttr=function{alert(this.attr);}




5.动态原型 (注意下面开始就是真正面向对象!!!)

function Bean{

this.attr= "";
this.getArray= Array;

//load 加载 时


(typeof Bean._initialized "und" ){

Bean.prototype.getAttr=function{alert(this.attr);};


Bean._initialized= true ;

}


}





/****************************************************************/


对象继承


1.对象冒充!!(可支持多继承山寨很强大)


function A(sstr){

this.color = sstr ;

this.sayColor = function{

alert(this.color);


};


}

function C{}


function B{

this.Method =ClassA ;

this.Method;

delete this.Method ;




this.Method =ClassC ;

this.Method;

delete this.Method ;



this.arrt = "google";


}




2.call apply 也山寨,

function A(sstr){

this.color = sstr ;

this.sayColor = function(str){

alert(str+this.color);


};


}


function B{

// this.Method =ClassA ;

// this.Method;

// delete this.Method ;

A.call(this,"red");

//A.apply(this, Array("red"))


this.arrt = "baidu";


}


3.正统继承 原型链 (但不支持多继承)
function A{this.oo="test";}
A.prototype.color = "red";
function B{}
B.prototype = A ;
B.prototype.sayName = function{
alert( this.color );
}
var bb = B ;
bb.sayName; // output red
alert(bb.oo); // output test

alert( bb instanceof A); //output true
alert( bb instanceof B); //output true
4.如果你要多继承!!并且还支持 instanceof
混合方式:
function A{}
function B{}
function C{
A.call(this);
C.call(this);
}
C.prototype = A ;//注意 这 instanceof 只能对 A有用

0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: