简体   繁体   English

无法让String.prototype在工厂工作

[英]Can't get String.prototype working in factory

So I'm trying to implement a function from an an answer here 所以我正在尝试从答案中实现一个功能

String.prototype.supplant = function (o) {
     return this.replace(/{([^{}]*)}/g,
         function (a, b) {
             var r = o[b];
             return typeof r === 'string' || typeof r === 'number' ? r : a;
         }
     );
 };

I've been trying to put this in a factory like this 我一直试图把它放在这样的工厂里

app.factory('Helpers', function() {
  abc = function (o) {
    return supplant(o);
  }

  abc.prototype.supplant = function (o) {
    return this.replace(/{([^{}]*)}/g,
        function (a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
  };

return abc;
});

I have used 'Helpers' in my controller, but keep getting errors like "I'm {age} years old!".abc is not a function 我在控制器中使用了“帮助器”,但不断收到诸如“我已经{age}岁!”之类的错误。

I need to access the function abc() just like shown in the answer like 我需要访问函数abc()就像答案所示

 alert("I'm {age} years old!".supplant({ age: 29 }));
 alert("The {a} says {n}, {n}, {n}!".supplant({ a: 'cow', n: 'moo' }));

You need to understand how things work before copy and paste code by hoping it will magically work... 您需要通过复制和粘贴代码来理解它的神奇作用,从而了解它们的工作原理。

In the example, the String.prototype is used to extend the default JavaScript String object (whatever surrounded by quotes is a String object), so after the following declaration: 在该示例中, String.prototype用于扩展 默认的 JavaScript String对象(无论用引号引起来都是String对象),因此在以下声明之后:

String.prototype.supplant = function (o) {
     return this.replace(/{([^{}]*)}/g,
         function (a, b) {
             var r = o[b];
             return typeof r === 'string' || typeof r === 'number' ? r : a;
         }
     );
 };

all strings in your code can invoke the method supplant() (in addition to the existing ones like toUppercase() , toLowerCase() ...). 在你的代码的所有字符串可以调用该方法supplant()除现有的像toUppercase() toLowerCase() ...)。

What you did in the angular factory is meaningless, you are not extending JavaScript's String object that way, but creating a brand new "type", so obviously by declaring: 您在angular工厂中所做的事情毫无意义,您不是在以这种方式扩展JavaScript的String对象,而是创建一个全新的“类型”,因此很明显地,它声明了:

"a string".supplant()

the interpreter complains about it since that method does not exist (by default JavaScript's String interface does not implement a method called supplant ). 解释器抱怨该方法不存在(默认情况下,JavaScript的String接口未实现名为supplant的方法)。

Anyway, since you are using angular, it already provides services for interpolation: https://docs.angularjs.org/api/ng/service/ $interpolate 无论如何,由于您正在使用角度,它已经提供了插值服务: https : //docs.angularjs.org/api/ng/service/ $ interpolate

So, inject that service in your controllers and use it, in this way: 因此,以这种方式将服务注入您的控制器并使用它:

$interpolate(stringToInterpolate)(contextScopeForInterpolation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM