Object is used by Prototype as a namespace; that is, it just keeps a few new methods together, which are intended for namespaced access (i.e. starting with “Object.”). 上面说的namespace个人理解就相当于C#中的静态类,提供工具函数的意思,和C#中的namespace应该不是一个概念。因为C#中的命名空间后面不会直接跟方法,肯定是接一个对象然后在调用方法,不过和C++中的命名空间倒是有些类似 clone extend inspect isArray isElement isFunction isHash isNumber isString isUndefined keys toHTML toJSON toQueryString values 复制代码 代码如下: //通过匿名函数调用的方式创建Object对象 (function() {
//取得类型的字符串表达形式,(Prototype 学习——工具函数学习($方法))这篇日志里有详细的说明 function getClass(object) { return Object.prototype.toString.call(object) .match(/^[objects(.*)]$/)[1]; }
//继承方法,非常简单的类抄写机制,就是把source里面的所有属性和方法复制一份到destination中,如果是引用类型,则source和destination将指向同一个地址 function extend(destination, source) { for (var property in source) destination[property] = source[property]; return destination; }
//返回object的字符串表达形式 function inspect(object) { try { if (isUndefined(object)) return "undefined"; if (object === null) return "null"; return object.inspect ? object.inspect() : String(object); } catch (e) { if (e instanceof RangeError) return "..."; throw e; } }
//返回object的JSON(JavaScript Object Notation) function toJSON(object) { var type = typeof object; switch (type) { case "undefined": case "function": case "unknown": return; case "boolean": return object.toString(); }
if (object === null) return "null"; if (object.toJSON) return object.toJSON(); if (isElement(object)) return;
var results = []; for (var property in object) { var value = toJSON(object[property]); if (!isUndefined(value)) results.push(property.toJSON() + ": " + value); }
return "{" + results.join(", ") + "}"; }
//返回查询字符串,例如:param1=value1¶m2=value2 function toQueryString(object) { return $H(object).toQueryString(); }