uTools-Manuals/docs/javascript/Reference/Errors/Deprecated_String_generics.html

81 lines
5.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<div></div>
<h2 id="错误提示">错误提示</h2>
<pre><code class="language-javascript">Warning: String.charAt is deprecated; use String.prototype.charAt instead
Warning: String.charCodeAt is deprecated; use String.prototype.charCodeAt instead
Warning: String.concat is deprecated; use String.prototype.concat instead
Warning: String.contains is deprecated; use String.prototype.contains instead
Warning: String.endsWith is deprecated; use String.prototype.endsWith instead
Warning: String.includes is deprecated; use String.prototype.includes instead
Warning: String.indexOf is deprecated; use String.prototype.indexOf instead
Warning: String.lastIndexOf is deprecated; use String.prototype.lastIndexOf instead
Warning: String.localeCompare is deprecated; use String.prototype.localeCompare instead
Warning: String.match is deprecated; use String.prototype.match instead
Warning: String.normalize is deprecated; use String.prototype.normalize instead
Warning: String.replace is deprecated; use String.prototype.replace instead
Warning: String.search is deprecated; use String.prototype.search instead
Warning: String.slice is deprecated; use String.prototype.slice instead
Warning: String.split is deprecated; use String.prototype.split instead
Warning: String.startsWith is deprecated; use String.prototype.startsWith instead
Warning: String.substr is deprecated; use String.prototype.substr instead
Warning: String.substring is deprecated; use String.prototype.substring instead
Warning: String.toLocaleLowerCase is deprecated; use String.prototype.toLocaleLowerCase instead
Warning: String.toLocaleUpperCase is deprecated; use String.prototype.toLocaleUpperCase instead
Warning: String.toLowerCase is deprecated; use String.prototype.toLowerCase instead
Warning: String.toUpperCase is deprecated; use String.prototype.toUpperCase instead
Warning: String.trim is deprecated; use String.prototype.trim instead
Warning: String.trimLeft is deprecated; use String.prototype.trimLeft instead
Warning: String.trimRight is deprecated; use String.prototype.trimRight instead
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p>警告。JavaScript 引擎不会停止运行。</p>
<h2 id="哪里出错了?">哪里出错了?</h2>
<p>非标准的泛型  <a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a> 方法已经被废弃,将来会被移除(这些方法仅在 Firefox 浏览器中得到实现。String 泛型在 String 对象上提供了一系列的 String 实例方法,使得这些 String 方法可以应用于任何类型的对象。</p>
<p>Firefox <a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1222552" rel="noopener" title="Remove String generics">bug 1222552</a> 对 String 泛型方法的移除进行了追踪。</p>
<h2 id="示例">示例</h2>
<h3 id="废弃的语法">废弃的语法</h3>
<pre><code class="language-js example-bad">var num = 15;
String.replace(num, /5/, '2');</code></pre>
<h3 id="标准语法">标准语法</h3>
<pre><code class="language-js example-good">var num = 15;
String(num).replace(/5/, '2');
</code></pre>
<h2 id="垫片">垫片</h2>
<p>以下是一个垫片脚本来为不支持 String 泛型方法浏览器提供支持:</p>
<pre><code class="language-javascript">/*globals define*/
// Assumes all supplied String instance methods already present
// (one may use shims for these if not available)
(function() {
'use strict';
var i,
// We could also build the array of methods with the following, but the
// getOwnPropertyNames() method is non-shimable:
// Object.getOwnPropertyNames(String).filter(function(methodName) {
// return typeof String[methodName] === 'function';
// });
methods = [
'contains', 'substring', 'toLowerCase', 'toUpperCase', 'charAt',
'charCodeAt', 'indexOf', 'lastIndexOf', 'startsWith', 'endsWith',
'trim', 'trimLeft', 'trimRight', 'toLocaleLowerCase', 'normalize',
'toLocaleUpperCase', 'localeCompare', 'match', 'search', 'slice',
'replace', 'split', 'substr', 'concat', 'localeCompare'
],
methodCount = methods.length,
assignStringGeneric = function(methodName) {
var method = String.prototype[methodName];
String[methodName] = function(arg1) {
return method.apply(arg1, Array.prototype.slice.call(arguments, 1));
};
};
for (i = 0; i &lt; methodCount; i++) {
assignStringGeneric(methods[i]);
}
}());</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a href="Reference/String" title="此页面仍未被本地化, 期待您的翻译!"><code>String</code></a></li>
<li><a href="Reference/Global_Objects/Array#Array_generic_methods" title="JavaScript的 Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。">Generics</a> 同样存在于 <a href="Reference/Array" title="REDIRECT Array"><code>Array</code></a> 的方法中(也同样被废弃了)。</li>
</ul>
</article>