String.prototype.format = function(args) {
if (arguments.length>0) {
var result = this;
if (arguments.length == 1 && typeof (args) == "object") {
for (var key in args) {
var reg=new RegExp ("({"+key+"})","g");
result = result.replace(reg, args[key]);
}
}
else {
for (var i = 0; i < arguments.length; i++) {
if(arguments[i]==undefined)
{
return "";
}
else
{
var reg=new RegExp ("({["+i+"]})","g");
result = result.replace(reg, arguments[i]);
}
}
}
return result;
}
else {
return this;
}
}

例:

复制代码 代码如下:

//两种调用方式
var template1="我是{0},今年{1}了";
var template2="我是{name},今年{age}了";
var result1=template1.format("loogn",22);
var result2=template1.format({name:"loogn",age:22});
//两个结果都是"我是loogn,今年22了"

JavaScript是基于对象的,任何元素都可以看成对象。然而,类型和对象是不同的。

本文中,我们除了讨论类型和对象的一些特点之外,更重要的是研究如何写出好的并且利于重用的类型。毕竟,JavaScript这种流行的脚本语言如果能够进行良好的封装,并形成一个庞大的类型库,对于重用是非常有意义的。
网上对于prototype的文章很多,一直没明白核心的思想。最后写了很多例子代码后才明白:prototype只能用在类型上。
以下是一些关于类型和对象的例子,大家看完例子后可能更容易理解类型和对象之间的联系:

1  使用proptotype为类型添加行为

Object.prototype.Property = 1;
Object.prototype.Method = function ()
{
    alert(1);
};
var obj = new Object();
alert(obj.Property);
obj.Method();

可以在类型上使用proptotype来为类型添加行为。这些行为只能在类型的实例上体现。 在实例上不能使用prototype,否则发生编译错误 .

JS中允许的类型有Array, Boolean, Date, Enumerator, Error, Function, Number, Object, RegExp, String

2  为类型定义“静态”的属性和方法

Object.Property = 1;
Object.Method = function()
{
    alert(1);
}
alert(Object.Property);
Object.Method();

可以为类型定义“静态”的属性和方法,直接在类型上调用即可 ,实例不能调用类型的静态属性或方法,否则发生对象未定义的错误。

3  JavaScript中定义一个类型

function Aclass()
{
this.Property = 1;
this.Method = function()
{
alert(1);
}
}
var obj = new Aclass();
alert(obj.Property);
obj.Method();

这个例子演示了通常的在JavaScript中定义一个类型的方法

4  使用prototype为自定义的类型增加属性和方法

[折叠代码]
function Aclass() { this.Property = 1; this.Method = function() { alert(1); } } Aclass.prototype.Property2 = 2; Aclass.prototype.Method2 = function() { alert(2); } var obj = new Aclass(); alert(obj.Property2); obj.Method2();
[折叠代码]

可以在外部使用prototype为自定义的类型添加属性和方法。 在外部不能通过prototype改变已有的自定义类型的属性或方法

5  在对象上改变已有的类型的属性和方法

[折叠代码]
function Aclass() { this.Property = 1; this.Method = function() { alert(1); } } var obj = new Aclass(); obj.Property = 2; obj.Method = function() { alert(2); } alert(obj.Property); obj.Method();
[折叠代码]

但可以在对象上改变已有的属性。(这个是肯定的),也可以在对象上改变已有的方法。(和普遍的面向对象的概念不同)

6   在对象上增加属性或方法

[折叠代码]
function Aclass() { this.Property = 1; this.Method = function() { alert(1); } } var obj = new Aclass(); obj.Property2 = 2; obj.Method2 = function() { alert(2); } alert(obj.Property2); obj.Method2();
[折叠代码]

还可以在对象上增加属性或方法

7 类型的继承

[折叠代码]
function AClass() { this.Property = 1; this.Method = function() { alert(1); } } function AClass2() { this.Property2 = 2; this.Method2 = function() { alert(2); } } AClass2.prototype = new AClass(); var obj = new AClass2(); alert(obj.Property); obj.Method(); alert(obj.Property2); obj.Method2();
[折叠代码]

这个例子说明了一个类型如何从另一个类型继承。

8  子类如何重写父类的属性或方法

[折叠代码]
function AClass() { this.Property = 1; this.Method = function() { alert(1); } } function AClass2() { this.Property2 = 2; this.Method2 = function() { alert(2); } } AClass2.prototype = new AClass(); AClass2.prototype.Property = 3; AClass2.prototype.Method = function() { alert(4); } var obj = new AClass2(); alert(obj.Property); obj.Method();
[折叠代码]

这个例子说明了子类如何重写父类的属性或方法。

可见JavaScript能够实现的面向对象的特征有:
·公有属性(public field)
·公有方法(public Method)
·私有属性(private field)
·私有方法(private field)
·方法重载(method overload)
·构造函数(constructor)
·事件(event)
·单一继承(single inherit)
·子类重写父类的属性或方法(override)
·静态属性或方法(static member)

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/czh_friend/archive/2007/04/16/1566319.aspx

最新文章

  1. python脚本实现scp上传下载功能
  2. ZAM 3D 制作3D动画字幕 用于Xaml导出
  3. java实现MD5加密
  4. 深入浅出设计模式——组合模式(Composite Pattern)
  5. 汇编中call printf参数压栈时错误理解
  6. ActionScript 3.0 编程精髓 示例源码下载
  7. try 返回前执行fianlly
  8. [King.yue]VS2012 无法启动IIS Express Web服务器的解决方案
  9. 20170709_python_学习记录
  10. java.lang.OutOfMemoryError 解决程序启动内存溢出问题
  11. 17.tslib安装以及使用
  12. Python猫荐书系列:文也深度学习,理也深度学习
  13. windows下面Nginx日志切割
  14. 《Java程序设计》 第四周学习总结
  15. 3D数学读书笔记——矩阵基础
  16. python post提交
  17. 使用telnet模拟邮件的收发
  18. Servlet下载文件迅雷不支持问题真相之一
  19. react中findDOMNode
  20. JodaTime报时区异常错误

热门文章

  1. 在Quartus II中分配管脚的两种常用方法
  2. c++ float 带 e 的指数
  3. Create,Insert
  4. 用@RequestMapping映射请求
  5. P2P小贷网站业务数据流程分享
  6. STORM_0008_Structure-of-the-codebase_Storm的代码库的结构
  7. CSS笔记(二)CSS属性选择器
  8. scala中如何编写自定义的流程控制结构
  9. NYOJ 士兵杀敌(三)
  10. mysql概要(五)union