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

js中String.prototype.format類似于.net中的string.formitz效果的更多相关文章

  1. js扩展String.prototype.format字符串拼接的功能

    1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...

  2. JavaScript中String.prototype.replace() 方法的使用

    摘抄于:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace ...

  3. js 实现 C# 的 format 方法

    2014-11-08 12:18:51 更新,修复原形链方法被当作关键词的bug,其实之前是想用全局关键词的,不过还是算了,array里有太多单词了.                          ...

  4. Javascript扩展String.prototype实现格式金额、格式时间、字符串连接、计算长度、是否包含、日期计算等功能

    <script src="Js/jquery-3.1.1.min.js"></script> <script type="text/java ...

  5. JavaScript 学习笔记 -- String.trim + format

    最近仍在IE6徘徊,低版本的浏览器没有实现JavaScript 的trim() 和 format(). . 主要是这两个使用的比较多,先整理出来: 1.trim() -- 去除字符串中开始和结尾部分, ...

  6. js中的prototype和__proto__

    var Person = function(name){ this.name = name; this.say = function(){ return "I am " + thi ...

  7. js中的prototype原型解析

    在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...

  8. JS中的prototype

    JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...

  9. JS中的prototype(原文地址:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html)

    JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...

随机推荐

  1. ProgressDialog 的 使用

    一 . ProgressDialog ProgressDialog是AlertDialog类的一个扩展,可以为一个未定义进度的任务显示一个旋转轮形状的进度动画,或者为一个指定进度的任务显示一个进度条. ...

  2. DbUtils使用时抛出Cannot get a connection

    java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object Caused by ...

  3. System.MissingMethodException: 找不到方法:

    This is a problem which can occur when there is an old version of a DLL still lingering somewhere ar ...

  4. Spark ML聚类分析之k-means||

    今天更新了电脑上的spark环境,因为上次运行新的流水线的时候,有的一些包在1.6.1中并不支持 只需要更改系统中用户的环境变量即可 然后在eclipse中新建pydev工程,执行环境是python3 ...

  5. STORM_0007_Multi-Lang protocol of Storm/多语言协议的翻译

    原始地址: http://storm.apache.org/releases/1.0.1/Multilang-protocol.html 这个协议试用0.7.1之后的版本   通过ShellBolt和 ...

  6. JPG 批量压缩、 PNG32、PNG24转PNG 透明批量压缩工具 【JPNG】 支持多级目录

    说在最前,压缩不一定是最好的,仅仅是为了方便自己工作需要.主要是手机端图片 算法说明:JPG压缩使用的是  adobe 的 JPGEncoder+ AIR的JPEGEncoderOptions (注 ...

  7. iOS - UIImagePickerController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImagePickerController : UINavigationController <NSCod ...

  8. iOS - UIView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppeara ...

  9. 如何读懂 STATSPACK 报告 (转) & Toad 结合

    可与 toad 相结合的内容, 用 这种颜色可以利用 toad(database->monitor->server statistics)查看到下边的很多信息, 比如 wait event ...

  10. Android开发把项目打包成apk

    做完一个Android项目之后,如何才能把项目发布到Internet上供别人使用呢?我们需要将自己的程序打包成Android安装包文件--APK(Android Package),其后缀名为" ...