简体   繁体   English

我应该在何处以及如何在Angular应用程序中扩展String原型?

[英]Where and how should I extend the String prototype in an Angular application?

I would like to extend the String prototype and use it in one or more components of my Angular 5 application. 我想扩展String原型并在我的Angular 5应用程序的一个或多个组件中使用它。 The main reason is that I have a series of objects, which have properties that sometimes are populated, and sometimes not. 主要原因是我有一系列对象,这些对象的属性有时会被填充,有时则不然。 Example: customer.address is sometimes a string (the _id of the address) and sometimes it's populated and it's an Address object, with an _id property. 示例: customer.address有时是一个string (地址的_id ),有时它是填充的,它是一个Address对象,带有_id属性。

What I want is basically not have to worry whether customer.address is populated or not, and simply access the _id . 我想要的基本上不用担心是否填充了customer.address,只需访问_id I know it's considered a Javascript anti-pattern according to some other responses here , but I thought of doing something like this: 我知道,它被认为是根据一些其他答复一个Javascript的反模式在这里 ,但我认为做这样的事情的:

String.prototype._id = function () {
    return String.prototype.valueOf()
}

Regardless of how bad of an idea this is -already answered in other questions-, would this be a way of doing it, and where should I declare this? 无论一个想法多么糟糕 - 这已经在其他问题中得到了解答 - 这是否是一种做到这一点的方式,我应该在哪里宣布这个? Is there a better way of solving this issue? 有没有更好的方法来解决这个问题?

Thanks. 谢谢。

PS. PS。 I know I could also overwrite the toString method in my Address class and get a similar result, calling customer.address.toString() to get the _id , but I feel like it's not legible and I'd like to explore other options first. 我知道我也可以覆盖我的Address类中的toString方法并获得类似的结果,调用customer.address.toString()来获取_id ,但我觉得它不易读,我想首先探索其他选项。

Based on this and my experience I would advise your to put like this, if your application is AngularJS : 根据这一点和我的经验,如果您的应用程序是AngularJS ,我会建议您这样做:

yourApp.run(function() { 
   String.prototype._id = function () {
     return String.prototype.valueOf()
   } 
}); 

For Angular 2+ you can check this and check this stackoverflow post that tells you how to extend String properties on Typescript. 对于角度2+您可以检查这个检查这个计算器后 ,告诉你如何在打字稿延长String类型的属性。

You might like to consider using a Proxy which wraps your object and provides a means to catch missing values and much more. 您可能想考虑使用包装对象的代理 ,并提供捕获缺失值的方法等等。

The example below shows how the desired functionality can be achieved without having to extend any built-in types. 下面的示例显示了如何在不扩展任何内置类型的情况下实现所需的功能。

 let customer1 = { address: 'some string' } let customer2 = { address: { _id: '12345' } } let proxyCustomer = customer => new Proxy(customer, { get: (target, name) => { // define the properties to catch and their functions let propHandlers = { address: () => { a = target.address // decide what you want to do // in this example a string value is returned as a dummy object with an _id. return typeof a === 'string' || a instanceof String ? {_id: a} : a } } // default action - return prop let def = () => target[name] // assign function to handleProp if it exists or use the default (return named prop) let handleProp = propHandlers[name] || def // return result of handle call return handleProp(); } }) // wrap customer objects in proxy customer1 = proxyCustomer(customer1) customer2 = proxyCustomer(customer2) // test console.log(customer1.address._id) console.log(customer2.address._id) 

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

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