Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203146
  • 博文数量: 73
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 750
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-13 18:32
文章分类

全部博文(73)

文章存档

2009年(1)

2008年(72)

我的朋友

分类: 系统运维

2008-04-06 10:19:32

 
1,对象的创建
  常规方法
  var obj=new Object();
  var func=new Function("x","return x*x;");
  对象直接量
  var circle={x:0,y:0,radius:2};
  var homer={
    name:"homer simpson",
    age:34,
    married:true,
    occupation:"plant operator",
    email:"homer@simpsons.com"
  }
2,未定义的对象属性&删除对象的属性
  读取对象一个不存在的属性,将返回一个未定义值undefined。
  js1.2中可以使用delete操作来删除对象的属性,而在之前的版本中该操作只是将属性的值设为null。
  甚至可以将一个对象的属性值设为未定义的,只需要将一个不存在的属性的值赋给它即可(假设当前有一个book.chapter2属性):
  book.chapter2=book.no_such_property;
  在js1.1中有一种更巧妙的方法使用void操作,将生成一个未定义的值:
  book.chapter2=void 0;
  注意将一个属性设置为未定义并不意味着将这个属性删除了。js1.2中的delete操作时唯一能将属性从对象中删除的方式:
 
3,对象属性的枚举
  使用for(var..in obj)来枚举对象的属性,没有特定的顺序,不会枚举所有预定义的属性和方法。
 
var student=new Object();
student.school="WHU";
student.name="abio";
student.home="JX";
delete student.home;
student.name=void 0;
var props="";
for(var prop in student){
    props+=prop+"\n";
}
alert(props);
#输出:school,home
 
4,构造函数 属性和方法
   属性:
   function Rectangle(w,h){
     this.width=w;
     this.heigth=h;
   }
   var rect1=new Rectangle(2,3);
   var rect2=new Rectangle(4.5,3.0);
   方法:
  
function Rectangle_area(){
  return this.width*this.heigth;
}
function Rectangle_perimeter(){
  return 2*this.width+2*this.heigth;
}
function Rectangle_set_size(w,h){
  this.width=2;this.height=h;
}

function Rectangle(w,h){
    this.width=w;
    this.height=h;
    
    this.area=Rectangle_area;
    this.perimeter=Rectangle_perimeter;
    this.set_size=Rectangle_set_size;
}

var r=new Rectangle(2,3);
var a=r.area;
r.enlarge();
var p=r.perimeter();
  以上例子中对方法的设置还是有缺陷的,三个方法都是常量(即它们应该是所有对象共享的)。每个初始化对象都会占用不必须的额外空间。js使用原型对象解决该问题。

5,原型对象,用于保存各个对象共享的方法和常量,避免不必要的copy.以下Circle类的定义是没有瑕疵的。

function Circle(x,y,r){
    this.x=x;
    this.y=y;
    this.r=r;
}
//new Circle(0,0,0)

Circle.prototype.PI=3.14149;
function Circle_circumference(){
    return 2*this.PI*this.r;
}

Circle.prototype.area=new
    Function("return this.PI*this.r*this.r;");
    
var c=new Circle(0.0,0.0,1.0);
var a=c.area();
var p=c.circumference();
 
  内置类的原型对象:不只是用户定义类型才具有原型对象。像String,Date这样的内置类也是具有原型对象的。
 
String.prototype.endsWith(c){
  return (c==this.charAt(this.length-1));
}
var meg="hello world";
meg.endsWith('h');//false
meg.endsWith('d');//true


6,对象的属性和方法
  1),constructor属性
  假设有一个类Complex:
  var o=new Complex(1,2);
  o.constructor==Complex
  constructor是对象从该类的原型对象中继承而来的。因此并不是每一个对象都有自己的一份拷贝。
  构造函数定义了一个类,所以使用constructor属性来确定一个对象的类型是十分方便的。
  if(typeof n=="object"&&n.constructor==Number){
    //使用Number。。。
  }
  但是并不能保证constructor总是存在的,这种情况发生在类的创建者使用一个具体的对象覆盖了之前的prototype对象。
  如:Number.prototype=new Number(13);
 
toString方法:
  将字符串对象使用+号链接,或者将对象传递给alert||document.write方法时toString方法会自动调用。
  另外默认的toString方法提供的信息不多,一般为|Object Object|
  c=new Circle(1,0,0)
  document.write(c)
  以上代码会依次在Circle.prototype,Object.prototype中查找toString最后使用的是Object.prototype.toString方法。
  若想使用toString得到更多关于对象的信息,可以通过以下的定义:
  Circle.prototype.toString=function(){...}
 
valueOf方法:
  与toString方法十分相似。当JavaScript需要将一个对象转换为字符串之外的原始类型时调用的就是它。
  因为大多数对象都没有等价的原始值(如number,string等)。因此Object类定义的valueOf方法并不执行任何有意义的转换。而Number和Boolean这样的类具有明显的原始值,故而它们覆盖了valueOf方法。
  注意:在某些环境中当需要从对象到字符串的转换时valueOf方法的优先级更高。这时就需要显式的调用toString方法。

阅读(1367) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~