日期:2014-05-16  浏览次数:20362 次

js基本语法总结

概括:

今天读了一下JavaScript权威指南,感觉到JavaScript的特有的优雅,它借鉴了n多其他语言的特性,实现了语法上的高度统一,的确简单就是美。

?

数据类型:

1、无论对于静态语言还是动态语言,类型始终是永恒的话题,有了类型我们才能向机器描述我们的数据,描述我们的操作从而达到描述我们要解决的问题的目的,只不过静态语言的类型需要我们自己去把握,而动态语言则尽可能的实现了自动化处理。

?

2、数值类型:借鉴了c#和Perl的处理方式,所有的数值类型在内部都表示为浮点数,但是数值类型可以进行自动的装箱操作。

3、bool类型:true 和 false。

4、字符串类型:"a good man"。

?

5、引用类型:以上三个基本类型以外的都是引用类型,引用类型的对象其实就是一个散列表。

?

引用类型详解:

?

1、创建一个引用类型对象有两种方式。

第一种方式:

var circle1={x:0,y:0,radius:2};

第二种方式:

function circle(x,y,radius)
{
	this.x=x;
	this.y=y;
	this.radius=radius;
}
circle1=new circle(0,0,2);

?以上两种方式产生的对象相同,从第二种创建引用对象的方法可以看出函数和对象的高度同意,因为函数本身就是对象,所以就把函数直接看成是一个引用类型的构造函数,实在是高。

?

2、对象的属性

对象的方法和对象的数据成员。

function Square(){return 3.14*this.radius*this.radius;}//注意这里的this
function Premeter(){return 6.28*this.radius;}

function circle(x,y,radius)
{
	this.x=x;
	this.y=y;
	this.radius=radius;
	this.square=Square;
}
circle1=new circle(0,0,3);
document.write(circle1.square()+"<br>");
circle1.premeter=Premeter;//因为对象就是散列表,你高兴什么时候加属性都ok。
document.write(circle1.premeter()+"<br>");

document.write("==============<br>");
for (var i in circle)
{
	document.write("value of "+i+" is: "+circle1.i+"<br>");
}
//函数对象只有一个属性prototype
document.write("==============<br>");
for (var i in circle1)
{
	document.write("value of "+i+" is: "+circle1.i+"<br>");
}
document.write("==============<br>");
delete circle1.premeter; //当然也可以删除其中的一个属性
for (var i in circle1)
{
	document.write("value of "+i+" is: "+circle1.i+"<br>");
}

if(circle1 instanceof circle)
{
	document.write("<br>circle1 is instance of circle!<br>");
}

?

3、类属性

circle.color="red";
document.write(circle1.color+"<br>");//undefined,类属性不能通过对象来访问。
circle.premeter=printinfo;//类函数成员中不能有this
circle.premeter();

?

4、继承

circle.prototype.printother=function(){document.write("other information<br>");}

function column(height)
{
	this.height=height;
}
column.prototype=new circle(10,10);
column1=new column(100);
column1.printother();
//查找顺序为函数成员--->prototype函数成员--->prototype里的函数成员。if(typeof column1 == "circle")
{
	document.write("<br>column1 is type of circle!<br>");//不会输出,因为所有的引用类型的都返回"object"
}

?继承是通过prototype属性来获得。

?

?

程序结构:

基本的分支if,循环while,do while,for和c一样。

break,continue和java一样结合标签label:使用。

异常处理:try{}catch{}finally{}。

?

?

?

?

?

?