您的位置:首页 > Web前端 > JavaScript

js中String.prototype.format類似于.net中的string.formitz效果

2016-02-02 12:00 579 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: