javascript之对象的种类、构成.使用、创建、属性及其他特性
参考了leadzen的blog:
http://www.cnblogs.com/leadzen/archive/2008/02/25/1073404.html
一、对象的种类
有两类:
1.非Function对象,也可以说是非function实体,我们将它称为普通对象吧
2.Function对象,也可以称之为function实体
那么Function对象和普通对象有什么区别呢?
答案是:Function对象可以像普通对象一样的被使用,当加上括号和参数时也可以当成函数来使用,当与new结合使用时可以生成普通对象
二、对象的构成.使用
(1)javascript的对象是由属性和Function对象属性构成的
(2)引用对象中的属性的方式和java是一样的,也是通过点(.)操作符来引用的
三、对象的创建
我们现在开始来阐述javascript的对象创建.共有4种方式.
1、json(JavaScript Object Notation)形式创建对象.这种形式适用于灵活性强的地方
例子1
<html><head> <title>generate object --gson form</title> <script type = "text/javascript"> //定义对象tester,通过json的形式 var tester = { //普通属性 attrB:true, //Function对象属性 action:function(){alert("hello world");} }; </script></head><body> <script type = "text/javascript"> //使用对象tester alert(tester.attrB); tester.action(); </script></body></html> Function functionname( ) { [native code] } <html><head> <title>Function object</title> <script type = "text/javascript"> var fun = function(){var str = "hello,world";alert(fun.caller);this.attrI = 123; } var fun2 = new Function("alert("hello,world");") </script></head><body> <script type = "text/javascript"> alert(fun.toString());//输出:function () {var str = "hello,world";alert(fun.caller);this.attrI = 123;}alert(fun2.toString());//输出:function anonymous() {alert("hello,world");} alert(Function.toString());//输出:function Function() {[native code]} </script></body></html>var obj = new Object;obj.attr1 = "1";obj.attr2 = "2";
var life = {}; for(life.age = 1; life.age <= 3; life.age++) { switch(life.age) { case 1: life.body = "卵细胞"; life.say = function(){alert(this.age+this.body)}; break; case 2: life.tail = "尾巴"; life.gill = "腮"; life.body = "蝌蚪"; life.say = function(){alert(this.age+this.body+"-"+this.tail+","+this.gill)}; break; case 3: delete life.tail; delete life.gill; life.legs = "四条腿"; life.lung = "肺"; life.body = "青蛙"; life.say = function(){alert(this.age+this.body+"-"+this.legs+","+this.lung)}; break; }; life.say(); };