String and Array generics methods will be deprecated in the future

两盒软妹~` 提交于 2019-12-07 06:47:36

问题


At the link below (MDN site) it says "String generics are non-standard, deprecated and might get removed in the future. Note that you can not rely on them cross-browser without using the shim that is provided below."

Are the methods they are referring to the methods listed in the shim they provide below this statement? This is the only reference to the phrase "String generics" I have seen so it's confusing me.

Also the same question for Array generics as the site mentions a similar situation for them as well.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#String_generic_methods

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Array_generic_methods


回答1:


Generic means "referring to all", and in this case it means the methods which are independent from the instances, i.e.

var foo = 'bar';
String.split(bar, 'a'); // "generic" method, non-standard, will throw ReferenceErrors
bar.split('a'); // instance method, standard

It is very unlikely you've written any code in the non-standard way as it already won't work on most people's browsers.


If you were using this way of accessing bar a method for type Foo to use them on Foo-like things, go via Foo.prototype.bar.call instead, i.e.

var baz = {length: 2, 0: 'fizz', 1: 'buzz'}; // Array-like
Array.slice(baz, 0, 1); // bad
Array.prototype.slice.call(baz, 0, 1); // good


来源:https://stackoverflow.com/questions/32172769/string-and-array-generics-methods-will-be-deprecated-in-the-future

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!