2016-09-20 7 views
2

文字列に連鎖プロトタイプを使用したいと思います。しかし、私が返す文字列プロトタイプはもう適用されません。 thisを元の方法で返すことなく修正する方法。ネイティブString.prototypeと同じです。文字列に連鎖プロトタイプを使用する方法

function StringUtil(str){ 
 
    this.str = str; 
 
} 
 

 
StringUtil.prototype.toCamelCase = function() { 
 
    return this.str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) { 
 
    return index === 0 ? letter.toLowerCase() : letter.toUpperCase(); 
 
    }).replace(/\s+/g, ''); 
 
}; 
 

 
StringUtil.prototype.toSlug = function() { 
 
    return this.str.toString().toLowerCase() 
 
    .replace(/\s+/g, '-')   // Replace spaces with - 
 
    .replace(/[^\w\-]+/g, '')  // Remove all non-word chars 
 
    .replace(/\-\-+/g, '-')   // Replace multiple - with single - 
 
    .replace(/^-+/, '')    // Trim - from start of text 
 
    .replace(/-+$/, '');   // Trim - from end of text 
 
}; 
 

 
var util = new StringUtil('this is a custom string'); 
 
console.log(util.toCamelCase()) // thisIsACustomString 
 
console.log(util.toSlug()) // this-is-a-custom-string 
 

 
util.toCamelCase().toSlug() // This does not work

答えて

1
StringUtil.prototype.toCamelCase = function() { 
    return this.str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) { 
    return index === 0 ? letter.toLowerCase() : letter.toUpperCase(); 
    }).replace(/\s+/g, ''); 
}; 

あなたはString.prototype.replace「リターン(文字列)と同じthis.str.replaceによって返された値を返しています。

次の機能と同じです。

1

問題は、新しいstring)を返しreplace(あなたがStringUtilメソッド内Stringオブジェクトを返しているということです。だから、文字列のプロトタイプを作成している新しいメソッドを持っていません。あなたが新しいStringUtilオブジェクトを返す、それを修正することができますあなたの新しいメソッド:。

function StringUtil(str){ 
    this.str = str; 
} 

StringUtil.prototype.toCamelCase = function() { 
    return new StringUtil(this.str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) { 
    return index === 0 ? letter.toLowerCase() : letter.toUpperCase(); 
    }).replace(/\s+/g, '')); 
}; 
+0

すべてのメソッドで新しい 'StringUtil'オブジェクトを返すと、パフォーマンスに影響しますか? – user6571640

+0

このケースでは何の問題もないと思います。とにかく@ gurvinder372の方法に従って、 'this'オブジェクトを返すことができます。 – ianaya89

1

あなたが動作するように連鎖するためにあなたの方法からthisを返す必要が

例えば

function StringUtil(str){ 
 
    this.str = str; 
 
} 
 

 
StringUtil.prototype.toCamelCase = function() { 
 
    this.str = this.str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(letter, index) { 
 
    return index === 0 ? letter.toLowerCase() : letter.toUpperCase(); 
 
    }).replace(/\s+/g, ''); 
 
    return this; 
 
}; 
 

 
StringUtil.prototype.toSlug = function() { 
 
    this.str = this.str.toString().toLowerCase() 
 
    .replace(/\s+/g, '-')   // Replace spaces with - 
 
    .replace(/[^\w\-]+/g, '')  // Remove all non-word chars 
 
    .replace(/\-\-+/g, '-')   // Replace multiple - with single - 
 
    .replace(/^-+/, '')    // Trim - from start of text 
 
    .replace(/-+$/, '');   // Trim - from end of text 
 
    return this; 
 
}; 
 

 

 
StringUtil.prototype.setStr =function(str) { 
 
    this.str = str; 
 
    return this; 
 
}; 
 

 
StringUtil.prototype.toString =function() { 
 
    return this.str.toString(); 
 
}; 
 

 

 
var util = new StringUtil('this is a custom string'); 
 
console.log(util.toCamelCase().toString()) 
 
console.log(util.setStr("this is a custom string").toSlug().toString()) 
 

 
console.log(util.toCamelCase().toSlug().toString())

私はチェーンの効果を説明するために、あなたのクラスにメソッドをいくつか追加しました。

  • のtoString() - あなたはthisを戻ってきているので、console.logが必要な形式でデータを印刷されません。
  • setStr() - 元の文字列に作用するtoSlugが表示されます。
関連する問題