阅读背景:

与string.replace;替换功能结果问题

来源:互联网 

I have this piece of code:

我有这段代码:

var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse(text) {
     return text.replace(/valid_pattern/gi, function (

I have this piece of code:

我有这段代码:

var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse(text) {
     return text.replace(/valid_pattern/gi, function ($1) { return this.complex($1); } );
   }
}

Of course calling this.complex($1) won't do the trick, because I'm in the scope of the anonymous function. I can't re-scope the anonymous function using .call(this) statement either, because in th ta case I would lose the parameters passed to the function by String.replace.

当然调用this.complex($ 1)将无法解决问题,因为我处于匿名函数的范围内。我也不能使用.call(this)语句重新定义匿名函数的范围,因为在这种情况下,我将失去String.replace传递给函数的参数。

So far I'm using the concrete instance of the object. This is my solution:

到目前为止,我正在使用该对象的具体实例。这是我的解决方案:

var instance = new myObj;
var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse(text) {
     return text.replace(/valid_pattern/gi, function ($1) { return instance.complex($1); } );
   }
}

So far it's sufficient to my needs, but I'm wondering if there is any universal solution to this problem. The only idea that has worked for me so far is this:

到目前为止,它足以满足我的需求,但我想知道是否有任何通用的解决方案来解决这个问题。到目前为止,唯一对我有用的想法是:

function ($1) { return (new myObj).complex($1); }

... which suffers from serious performance issues. Any ideas would be greatly appreciated.

......遭遇严重的性能问题。任何想法将不胜感激。

-- D.

P. S. Sorry about my English, it's not my first language.

P. S.对我的英语很抱歉,这不是我的第一语言。

3 个解决方案

#1


Maybe try:

var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse(text) {
     var that = this;
     return text.replace(/valid_pattern/gi, function ($1) { return that.complex($1); } );
   }
}

It is one of the most useful tricks :-)

这是最有用的技巧之一:-)

UPDATE: The trick is not mine, I learned it (as most of things I know about Javascript) from: Douglas Crockford

更新:诀窍不是我的,我从以下方面学到了它(我所知道的关于Javascript的大部分内容):Douglas Crockford

#2


This is what prototype and others do

这是原型和其他人所做的

// Monkey Patching, not everyone likes it
Function.prototype.bind = function( obj ) {
    var _this = this;
    return function() {
        return _this.apply( obj, arguments )
    }
}

Now you can do this

现在你可以做到这一点

var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse = function(text) {
     return text.replace(/valid_pattern/gi, function ($1) { return this.complex($1); }.bind( this ) );
   }
}

O = new myObj();
alert( O.parse( 'some text' );

#3


declare a variable for that.

为此声明一个变量。

var myObj = function () {
  var foo = this.complex = function (text) { /* long piece of code */ }
  this.parse(text) {
    return text.replace(/valid_pattern/gi, foo );
  }
}

) { return this.complex(

I have this piece of code:

我有这段代码:

var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse(text) {
     return text.replace(/valid_pattern/gi, function ($1) { return this.complex($1); } );
   }
}

Of course calling this.complex($1) won't do the trick, because I'm in the scope of the anonymous function. I can't re-scope the anonymous function using .call(this) statement either, because in th ta case I would lose the parameters passed to the function by String.replace.

当然调用this.complex($ 1)将无法解决问题,因为我处于匿名函数的范围内。我也不能使用.call(this)语句重新定义匿名函数的范围,因为在这种情况下,我将失去String.replace传递给函数的参数。

So far I'm using the concrete instance of the object. This is my solution:

到目前为止,我正在使用该对象的具体实例。这是我的解决方案:

var instance = new myObj;
var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse(text) {
     return text.replace(/valid_pattern/gi, function ($1) { return instance.complex($1); } );
   }
}

So far it's sufficient to my needs, but I'm wondering if there is any universal solution to this problem. The only idea that has worked for me so far is this:

到目前为止,它足以满足我的需求,但我想知道是否有任何通用的解决方案来解决这个问题。到目前为止,唯一对我有用的想法是:

function ($1) { return (new myObj).complex($1); }

... which suffers from serious performance issues. Any ideas would be greatly appreciated.

......遭遇严重的性能问题。任何想法将不胜感激。

-- D.

P. S. Sorry about my English, it's not my first language.

P. S.对我的英语很抱歉,这不是我的第一语言。

3 个解决方案

#1


Maybe try:

var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse(text) {
     var that = this;
     return text.replace(/valid_pattern/gi, function ($1) { return that.complex($1); } );
   }
}

It is one of the most useful tricks :-)

这是最有用的技巧之一:-)

UPDATE: The trick is not mine, I learned it (as most of things I know about Javascript) from: Douglas Crockford

更新:诀窍不是我的,我从以下方面学到了它(我所知道的关于Javascript的大部分内容):Douglas Crockford

#2


This is what prototype and others do

这是原型和其他人所做的

// Monkey Patching, not everyone likes it
Function.prototype.bind = function( obj ) {
    var _this = this;
    return function() {
        return _this.apply( obj, arguments )
    }
}

Now you can do this

现在你可以做到这一点

var myObj = function () {
   this.complex = function (text) { /* long piece of code */ }
   this.parse = function(text) {
     return text.replace(/valid_pattern/gi, function ($1) { return this.complex($1); }.bind( this ) );
   }
}

O = new myObj();
alert( O.parse( 'some text' );

#3


declare a variable for that.

为此声明一个变量。

var myObj = function () {
  var foo = this.complex = function (text) { /* long piece of code */ }
  this.parse(text) {
    return text.replace(/valid_pattern/gi, foo );
  }
}

); } ); } } var myOb



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: