javascript”面向对象编程”- 1万物皆对象
2009-09-16 22:17
387 查看
http://www.cnblogs.com/heros/archive/2009/09/14/1566622.html
javascript几乎成了如今web开发人员必学必会的一门语言,但很多人却只停在了一些表单验证等基础操作层面上,在面向对象语言大行其道的当下,我们需要去学习javascript的面向对象的知识,以便更好的掌握javascript、为深入理解各种脚本框架打好基础。
javascript和java、C#等语言一样也具有面向对象的一些特征,但细比较的时候,会发现这些特征并不是真正的面向对象,很多地方都是利用对象本身来模拟面向对象,所以认为javascript不能算是面向对象编程语言,而是基于对象的语言。
在javascript中真的是万物皆对象,new出来的东西是对象,方法是对象,连类也都是对象。下面分别来看一下对象、方法和类的对象特征。
1.拿内置的Date来看一下吧
var time = new Date();
var timeString = time.getFullYear() + "-" +
time.getMonth() + "-" +
time.getDate() + " " +
time.getHours() + ":" +
time.getMinutes() + ":" +
time.getSeconds();
document.write(timeString);
通过 time来操作其所引用的Date对象,可以方便的调用Date的对象所包含的一系列getXX()方法来获取年月日时分秒等信息。
可以再看一下String
var username = new String("hello world");
document.write(username.length);
变量username引用了new出来的字符串对象,通过username访问字符串对象的length属性。
2.方法也是对象
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello是一个方法,helloRef是一个引用了hello方法的变量,helloRef和hello一样都指向了相同的方法对象。也就意味着helloRef也可以执行,helloRef()。同理也可以写出以下代码。
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert(“hello”)}是一个匿名方法,当然也是对象,用helloRef变量引用该方法对象后,可以通过helloRef来调用方法。
3.那么类呢?当然类也是对象,在javascript中,不像C#或java那样有class关键字用来创建类,而是直接使用方法的关键字来创建类或者叫模拟类。
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "岁了。");
};
};
var person1 = new Person("张三", 20);
person1.Introduce();
以上创建了一个Person类型,Person带有构造参数username和age,通过创建的Person对象可以调用Person所包含的方法Introduce。下面对代码做一些修改。
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "岁了。");
};
};
var PersonClass = Person;
var person1 = new PersonClass("张三", 20);
person1.Introduce();
重新声明新的变量PersonClass并引用Person类,PersonClass和Person都指向了原来的Person所引用的类,所以也可以用PersonClass来创建对象。
以上的几个例子可能不是很恰当,但也可以一窥javascript中万物皆对象。
下一节详细的谈一谈javascript中的对象。
javascript几乎成了如今web开发人员必学必会的一门语言,但很多人却只停在了一些表单验证等基础操作层面上,在面向对象语言大行其道的当下,我们需要去学习javascript的面向对象的知识,以便更好的掌握javascript、为深入理解各种脚本框架打好基础。
javascript和java、C#等语言一样也具有面向对象的一些特征,但细比较的时候,会发现这些特征并不是真正的面向对象,很多地方都是利用对象本身来模拟面向对象,所以认为javascript不能算是面向对象编程语言,而是基于对象的语言。
在javascript中真的是万物皆对象,new出来的东西是对象,方法是对象,连类也都是对象。下面分别来看一下对象、方法和类的对象特征。
1.拿内置的Date来看一下吧
var time = new Date();
var timeString = time.getFullYear() + "-" +
time.getMonth() + "-" +
time.getDate() + " " +
time.getHours() + ":" +
time.getMinutes() + ":" +
time.getSeconds();
document.write(timeString);
通过 time来操作其所引用的Date对象,可以方便的调用Date的对象所包含的一系列getXX()方法来获取年月日时分秒等信息。
可以再看一下String
var username = new String("hello world");
document.write(username.length);
变量username引用了new出来的字符串对象,通过username访问字符串对象的length属性。
2.方法也是对象
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello是一个方法,helloRef是一个引用了hello方法的变量,helloRef和hello一样都指向了相同的方法对象。也就意味着helloRef也可以执行,helloRef()。同理也可以写出以下代码。
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert(“hello”)}是一个匿名方法,当然也是对象,用helloRef变量引用该方法对象后,可以通过helloRef来调用方法。
3.那么类呢?当然类也是对象,在javascript中,不像C#或java那样有class关键字用来创建类,而是直接使用方法的关键字来创建类或者叫模拟类。
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "岁了。");
};
};
var person1 = new Person("张三", 20);
person1.Introduce();
以上创建了一个Person类型,Person带有构造参数username和age,通过创建的Person对象可以调用Person所包含的方法Introduce。下面对代码做一些修改。
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "岁了。");
};
};
var PersonClass = Person;
var person1 = new PersonClass("张三", 20);
person1.Introduce();
重新声明新的变量PersonClass并引用Person类,PersonClass和Person都指向了原来的Person所引用的类,所以也可以用PersonClass来创建对象。
以上的几个例子可能不是很恰当,但也可以一窥javascript中万物皆对象。
下一节详细的谈一谈javascript中的对象。
相关文章推荐
- javascript”面向对象编程”- 1万物皆对象
- javascript 面向对象编程 万物皆对象
- javascript 面向对象编程 万物皆对象
- Javascript面向对象编程 产生对象
- javascript面向对象编程的学习---对象继承
- JavaScript面向对象编程之创建对象
- javascript”面向对象编程”- 2聊聊对象的事
- JavaScript面向对象编程(1)简单对象
- 韩顺平 javascript教学视频_学习笔记12_js面向对象编程介绍_类(原型对象)和对象
- JavaScript 面向对象编程,严格过程的标准化编程法,目前为止最好的 JS 生成对象代码结构
- javascript面向对象编程的学习---对象继承
- 韩顺平_轻松搞定网页设计(html+css+javascript)_第29讲_二维数组转置_js面向对象编程介绍 类(原型对象)和对象_学习笔记_源代码图解_PPT文档整理
- Javascript之面向对象编程(创建对象)
- JavaScript面向对象编程(2)对象上的特殊属性
- Javascript面向对象编程——对象继承的写法
- 面向对象编程,C#,Java中都有this,this代表了当前的对象,那么JavaScript中this代表什么
- JavaScript 面向对象编程,严格过程的标准化编程法,目前为止最好的 JS 生成对象代码结构
- javascript是脚本语言?javascript万物皆对象?
- javascript 面向对象编程 聊聊对象的事
- JavaScript第四天学习--万物皆对象