看一个简单的例子:

<script type="text/javascript">
<script type="text/javascript">
// 以下是对函数,对象,对象方法,对象属性及原型作一个简单的实例说明:
var test = test1 = test2 = function (width, height) { //同时建立多个函数(指定同一个对象的对象方法的实例)
return new test.fn.init(width, height);
};
test.prototype = test.fn = {
init: function (width, height) { alert("作者:" + this.Author + "宽为:" + width + ",高为:" + height); },//对象方法(函数)
Author: "张占岭(Lose)" //对象属性
};

test(800, 600);
test1(400, 300);

</script>

再来看一个访jquery的选择器是如何使用它们的:

<script type="text/javascript">
<script type="text/javascript">



// Jquery的选择器是怎么炼成的.
// 作者:张占岭
// 相关知识点:JS对象,原型,JS正则表达式,对象方法,对象属性等
// 定义变量 undefined 方便使用
var zzlQuery = window.zzlQuery = function (selector, context) { //定义一个函数zzlQuery
return new zzlQuery.fn.init(selector, context); //函数返回一个对象
},
toString = Object.prototype.toString, //object 等于object的原型
idExpr = /^#([\w-]+)$/; //查询是否为一个ID
// 设置 jQuery 的原型对象, 用于所有 jQuery 对象共享
zzlQuery.fn = zzlQuery.prototype = { // #74
length: 0, // #190
version: "1.0.0", // # 187
// 这是一个示例,仅仅提供两种选择方式:id 和标记名
init: function (selector, context) { // #75
// Handle HTML strings
if (typeof selector === "string") {
// Are we dealing with HTML string or an ID?
match = idExpr.exec(selector); //执行一个正则表达式
// Verify a match, and that no context was specified for #id
if (match && match[1]) {
var elem = document.getElementById(match[1]);
if (elem) {
this.length = 1;
this[0] = elem;
}
}
else {
// 直接使用标记名
var nodes = document.getElementsByTagName(selector);
for (var l = nodes.length, j = 0; j < l; j++) {
this[j] = nodes[j];
}
this.length = nodes.length;
}
this.context = document;
this.selector = selector;
return this;
}
},

// 代表的 DOM 对象的个数
size: function () {
return this.length;
},
// 用来设置 css 样式
css: function (name, value) {
this.each(
function (name, value) {
this.style[name] = value;
},
arguments // 实际的参数以数组的形式传递
);
return this;
},
// 用来设置文本内容
text: function (val) {
if (val) {
this.each(function () {
this.innerHTML = val;
},
arguments // 实际的参数以数组的形式传递
)
}
return this;
},
// 用来对所有的 DOM 对象进行操作
// callback 自定义的回调函数
// args 自定义的参数
each: function (callback, args) { // #244
return zzlQuery.each(this, callback, args);
}
}
// init 函数的原型也就是 jQuery 的原型
zzlQuery.fn.init.prototype = zzlQuery.prototype; // #303
// 用来遍历 jQuery 对象中包含的元素
zzlQuery.each = function (object, callback, args) { // #550
var i = 0, length = object.length;
// 没有提供参数
if (args === undefined) {
for (var value = object[0];
i < length && callback.call(value, i, value) !== false;
value = object[++i])
{ }
}
else {
for (; i < length; ) {
if (callback.apply(object[i++], args) === false) {
break;
}
}
}
}
zzlQuery("h2").text("Hello, world.").css("color", "red"); //调用涵数


</script>

看完之后是不是你也想说:哇,原来JS也能面向对象呀!

作者:仓储大叔,张占岭,
荣誉:微软MVP