diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:41:15 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:41:15 -0500 |
commit | 4b1a9203c547c019fc5398082ae19a3f3d4c3efe (patch) | |
tree | d4a40e13ceeb9f85479605110a76e7a4d5f3b56b /files/ca/web/javascript/reference | |
parent | 33058f2b292b3a581333bdfb21b8f671898c5060 (diff) | |
download | translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.gz translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.tar.bz2 translated-content-4b1a9203c547c019fc5398082ae19a3f3d4c3efe.zip |
initial commit
Diffstat (limited to 'files/ca/web/javascript/reference')
45 files changed, 6865 insertions, 0 deletions
diff --git a/files/ca/web/javascript/reference/errors/index.html b/files/ca/web/javascript/reference/errors/index.html new file mode 100644 index 0000000000..c295fccea6 --- /dev/null +++ b/files/ca/web/javascript/reference/errors/index.html @@ -0,0 +1,31 @@ +--- +title: JavaScript error reference +slug: Web/JavaScript/Reference/Errors +tags: + - Debugging + - Error + - Errors + - Exception + - JavaScript + - NeedsTranslation + - TopicStub + - exceptions +translation_of: Web/JavaScript/Reference/Errors +--- +<p>{{jsSidebar("Errors")}}</p> + +<p>Below, you'll find a list of errors which are thrown by JavaScript. These errors can be a helpful debugging aid, but the reported problem isn't always immediately clear. The pages below will provide additional details about these errors. Each error is an object based upon the {{jsxref("Error")}} object, and has a <code>name</code> and a <code>message</code>.</p> + +<p>Errors displayed in the Web console may include a link to the corresponding page below to help you quickly comprehend the problem in your code.</p> + +<h2 id="List_of_errors">List of errors</h2> + +<p>In this list, each page is listed by name (the type of error) and message (a more detailed human-readable error message). Together, these two properties provide a starting point toward understanding and resolving the error. For more information, follow the links below!</p> + +<p>{{ListSubPages("/en-US/docs/Web/JavaScript/Reference/Errors")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Learn/JavaScript/First_steps/What_went_wrong">What went wrong? Troubleshooting JavaScript</a>: Beginner's introductory tutorial on fixing JavaScript errors.</li> +</ul> diff --git a/files/ca/web/javascript/reference/errors/nomes-lectura/index.html b/files/ca/web/javascript/reference/errors/nomes-lectura/index.html new file mode 100644 index 0000000000..30c70c40dd --- /dev/null +++ b/files/ca/web/javascript/reference/errors/nomes-lectura/index.html @@ -0,0 +1,78 @@ +--- +title: 'TipusError: "x" es només de lectura' +slug: Web/JavaScript/Reference/Errors/Nomes-Lectura +tags: + - Errors + - JavaScript + - TypeError +translation_of: Web/JavaScript/Reference/Errors/Read-only +--- +<div>{{jsSidebar("Errors")}}</div> + +<h2 id="Missatge">Missatge</h2> + +<pre class="syntaxbox">TipusError: "x" es només de lectura (Firefox) +TipusError: 0 es només de lectura (Firefox) +TipusError: No es pot fer l'assignació a la propietat 'x' de #<Object> que es només de lectura (Chrome) +TipusError: No es pot fer l'assignació a la propietat '0' de [object Array] (Chrome) +</pre> + +<h2 id="Tipus_d'error">Tipus d'error</h2> + +<p>{{jsxref("TypeError")}}</p> + +<h2 id="Qué_ha_anat_malament">Qué ha anat malament?</h2> + +<p>La variable global o propietat de l'objecte a la qual s'ha volgut fer l'assignació es només de lectura. (Tècnicament, es una <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Writable_attribute">propietat de no-escriptura</a>.)</p> + +<p>Aquest error succeeix només en <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">codi en mode estricte</a>. En codi en mode no estricte, l'assignació es ignorada de manera silenciosa.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Casos_invàlids">Casos invàlids</h3> + +<p>Propietats de només lectura no son súper comuns, però es poden donar quan es fa servir {{jsxref("Object.defineProperty()")}} o {{jsxref("Object.freeze()")}}.</p> + +<pre class="brush: js example-bad">'use strict'; +var obj = Object.freeze({name: 'Elsa', score: 157}); +obj.score = 0; // TypeError + +'use strict'; +Object.defineProperty(this, 'LUNG_COUNT', {value: 2, writable: false}); +LUNG_COUNT = 3; // TypeError + +'use strict'; +var frozenArray = Object.freeze([0, 1, 2]); +frozenArray[0]++; // TypeError +</pre> + +<p>També hi ha unes poques propietats de només lectura en la construcció de JavaScript. Potser que hagis provat de redefinir una constant matemàtica.</p> + +<pre class="brush: js example-bad">'use strict'; +Math.PI = 4; // TypeError</pre> + +<p>Ho sentim, no pots fer això.</p> + +<p>La variable global <code>undefined</code> també es només de lectura, llavors no pots silenciar l'infame error "undefined no es una funció" fent això:</p> + +<pre class="brush: js example-bad">'use strict'; +undefined = function() {}; // TypeError: "undefined" es només de lectura +</pre> + +<h3 id="Valid_cases">Valid cases</h3> + +<pre class="brush: js example-good">'use strict'; +var obj = Object.freeze({name: 'Score', points: 157}); +obj = {name: obj.name, points: 0}; // reemplaçant-ho amb un nou objecte funciona + +'use strict'; +var LUNG_COUNT = 2; // Una `var` funciona, perque no es de només lectura +LUNG_COUNT = 3; // ok (anatòmicament potser, però poc probable) +</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Object.defineProperty()")}}</li> + <li>{{jsxref("Object.freeze()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/functions/arguments/caller/index.html b/files/ca/web/javascript/reference/functions/arguments/caller/index.html new file mode 100644 index 0000000000..b0a6afdf3e --- /dev/null +++ b/files/ca/web/javascript/reference/functions/arguments/caller/index.html @@ -0,0 +1,93 @@ +--- +title: arguments.caller +slug: Web/JavaScript/Reference/Functions/arguments/caller +translation_of: Archive/Web/JavaScript/arguments.caller +--- +<div>{{jsSidebar("Functions")}}</div> + +<p>La propietat obsoleta <strong><code>arguments.caller</code></strong> solia proporcionar la funció que invoca la funció que s'està executant en aquest moment. Aquesta propietat s'ha eleminitat i ja no funciona.</p> + +<h2 id="Descripció">Descripció</h2> + +<p>La propietat ja no és troba disponible, però encara es pot utilitzar {{jsxref("Function.caller")}}.</p> + +<pre class="brush: js">function whoCalled() { + if (whoCalled.caller == null) + console.log('I was called from the global scope.'); + else + console.log(whoCalled.caller + ' called me!'); +}</pre> + +<h2 id="Exemples">Exemples</h2> + +<p>El codi següent s'utilitzava per comprovar el valor de <code>arguments.caller</code> en una funció, però ja no funciona.</p> + +<pre class="brush: js">function whoCalled() { + if (arguments.caller == null) + console.log('I was called from the global scope.'); + else + console.log(arguments.caller + ' called me!'); +} +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<p>No forma part de cap estàndard. Implementat en JavaScript 1.1 i eliminat en {{bug(7224)}} a causa una potencial vulnerabilitat de seguretat.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Function")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/functions/arguments/index.html b/files/ca/web/javascript/reference/functions/arguments/index.html new file mode 100644 index 0000000000..da5237bdf0 --- /dev/null +++ b/files/ca/web/javascript/reference/functions/arguments/index.html @@ -0,0 +1,211 @@ +--- +title: Arguments object +slug: Web/JavaScript/Reference/Functions/arguments +tags: + - Functions + - JavaScript + - NeedsTranslation + - TopicStub + - arguments +translation_of: Web/JavaScript/Reference/Functions/arguments +--- +<div> +<div>{{jsSidebar("Functions")}}</div> +</div> + +<p>The <strong><code>arguments</code></strong> object is an <code>Array</code>-like object corresponding to the arguments passed to a function.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox">arguments</pre> + +<h2 id="Description">Description</h2> + +<p>The <code>arguments</code> object is a local variable available within all functions. <code>arguments</code> as a property of <code>Function</code> can no longer be used.</p> + +<p>You can refer to a function's arguments within the function by using the <code>arguments</code> object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0. For example, if a function is passed three arguments, you can refer to the argument as follows:</p> + +<pre class="brush: js">arguments[0] +arguments[1] +arguments[2] +</pre> + +<p>The arguments can also be set:</p> + +<pre class="brush: js">arguments[1] = 'new value';</pre> + +<p>The <code>arguments</code> object is not an {{jsxref("Array")}}. It is similar to an <code>Array</code>, but does not have any <code>Array</code> properties except <code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/length" title="JavaScript/Reference/Functions_and_function_scope/arguments/length">length</a></code>. For example, it does not have the <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop" title="JavaScript/Reference/Global_Objects/Array/pop">pop</a></code> method. However it can be converted to a real <code>Array</code>:</p> + +<pre class="brush: js">var args = Array.prototype.slice.call(arguments);</pre> + +<div class="warning"> +<p><strong>Important:</strong> You should not slice on arguments because it prevents optimizations in JavaScript engines (V8 for example). Instead, try constructing a new array by iterating through the arguments object. <a href="https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments">More information</a>.</p> +</div> + +<p>If <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Array_generic_methods" title="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array#Array_generic_methods">Array generics</a> are available, one can use the following instead:</p> + +<pre class="brush: js">var args = Array.slice(arguments);</pre> + +<p>The <code>arguments</code> object is available only within a function body. Attempting to access the <code>arguments</code> object outside a function declaration results in an error.</p> + +<p>You can use the <code>arguments</code> object if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments. You can use <code><a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/length" title="JavaScript/Reference/Functions_and_function_scope/arguments/length">arguments.length</a></code> to determine the number of arguments passed to the function, and then process each argument by using the <code>arguments</code> object. (To determine the number of arguments declared when a function was defined, use the <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function/length" title="JavaScript/Reference/Global_Objects/Function/length">Function.length</a></code> property.)</p> + +<h2 id="Properties">Properties</h2> + +<dl> + <dt><code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee" title="JavaScript/Reference/Functions_and_function_scope/arguments/callee">arguments.callee</a></code></dt> + <dd>Reference to the currently executing function.</dd> + <dt><code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/caller" title="JavaScript/Reference/Functions_and_function_scope/arguments/caller">arguments.caller</a></code> {{ Obsolete_inline() }}</dt> + <dd>Reference to the function that invoked the currently executing function.</dd> + <dt><code><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments/length" title="JavaScript/Reference/Functions_and_function_scope/arguments/length">arguments.length</a></code></dt> + <dd>Reference to the number of arguments passed to the function.</dd> +</dl> + +<h2 id="Examples">Examples</h2> + +<h3 id="Defining_a_function_that_concatenates_several_strings">Defining a function that concatenates several strings</h3> + +<p>This example defines a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:</p> + +<pre class="brush:js">function myConcat(separator) { + var args = Array.prototype.slice.call(arguments, 1); + return args.join(separator); +}</pre> + +<p>You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.</p> + +<pre class="brush:js">// returns "red, orange, blue" +myConcat(", ", "red", "orange", "blue"); + +// returns "elephant; giraffe; lion; cheetah" +myConcat("; ", "elephant", "giraffe", "lion", "cheetah"); + +// returns "sage. basil. oregano. pepper. parsley" +myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");</pre> + +<h3 id="Defining_a_function_that_creates_HTML_lists">Defining a function that creates HTML lists</h3> + +<p>This example defines a function that creates a string containing HTML for a list. The only formal argument for the function is a string that is "<code>u</code>" if the list is to be unordered (bulleted), or "<code>o</code>" if the list is to be ordered (numbered). The function is defined as follows:</p> + +<pre class="brush:js">function list(type) { + var result = "<" + type + "l><li>"; + var args = Array.prototype.slice.call(arguments, 1); + result += args.join("</li><li>"); + result += "</li></" + type + "l>"; // end list + + return result; +}</pre> + +<p>You can pass any number of arguments to this function, and it adds each argument as an item to a list of the type indicated. For example:</p> + +<pre class="brush:js">var listHTML = list("u", "One", "Two", "Three"); + +/* listHTML is: + +"<ul><li>One</li><li>Two</li><li>Three</li></ul>" + +*/</pre> + +<h3 id="Rest_default_and_destructured_parameters">Rest, default and destructured parameters</h3> + +<p>The <code>arguments</code> object can be used in conjunction with <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">rest parameters</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters">default parameters</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">destructured parameters</a>.</p> + +<pre class="brush: js">function foo(...args) { + return arguments; +} +foo(1, 2, 3); // { "0": 1, "1": 2, "2": 3 } +</pre> + +<p>However, in non-strict functions, a <strong>mapped <code>arguments</code> object</strong> is only provided if the function does <strong>not</strong> contain any <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">rest parameters</a>, any <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters">default parameters</a> or any <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">destructured parameters</a>. For example, in the following function that uses a default parameter, <code>1</code>0 instead of 100 is returned:</p> + +<pre class="brush: js">function bar(a=1) { + arguments[0] = 100; + return a; +} +bar(10); // 10 +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Implemented in JavaScript 1.1</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-10.6', 'Arguments Object')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-arguments-exotic-objects', 'Arguments Exotic Objects')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Function")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/functions/arguments/length/index.html b/files/ca/web/javascript/reference/functions/arguments/length/index.html new file mode 100644 index 0000000000..cf2660b7e4 --- /dev/null +++ b/files/ca/web/javascript/reference/functions/arguments/length/index.html @@ -0,0 +1,117 @@ +--- +title: arguments.length +slug: Web/JavaScript/Reference/Functions/arguments/length +translation_of: Web/JavaScript/Reference/Functions/arguments/length +--- +<div>{{jsSidebar("Functions")}}</div> + +<p>La propietat <strong><code>arguments.length</code></strong> conté el número d'arguments passats a la funció.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox">arguments.length</pre> + +<h2 id="Descripció">Descripció</h2> + +<p>La propietat arguments.length proporciona el número d'arguments passats a la funció. Aquest pot ser major o menor que el nombre total de paràmetres definits. (Vegeu {{jsxref("Function.length")}}).</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_arguments.length">Utilitzar <code>arguments.length</code></h3> + +<p>En aquest exemple definim una funció que pot afegir dos o més nombres.</p> + +<pre class="brush: js">function adder(base /*, n2, ... */) { + base = Number(base); + for (var i = 1; i < arguments.length; i++) { + base += Number(arguments[i]); + } + return base; +} +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Definició inicial. Implementat en JavaScript 1.1</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-10.6', 'Arguments Object')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-arguments-exotic-objects', 'Arguments Exotic Objects')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Function")}}</li> + <li>{{jsxref("Function.length")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/functions/get/index.html b/files/ca/web/javascript/reference/functions/get/index.html new file mode 100644 index 0000000000..92a9de2965 --- /dev/null +++ b/files/ca/web/javascript/reference/functions/get/index.html @@ -0,0 +1,217 @@ +--- +title: getter +slug: Web/JavaScript/Reference/Functions/get +translation_of: Web/JavaScript/Reference/Functions/get +--- +<div>{{jsSidebar("Functions")}}</div> + +<p>la sintaxi <strong><code>get</code></strong> lliga la propietat d'un objecte a una funció que es cridarà quan la propietat sigui cercada.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox">{get <em>prop</em>() { ... } } +{get <em>[expressió]</em>() { ... } }</pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>prop</code></dt> + <dd>El nom de la propietat que es pretén lligar a la funció donada.</dd> + <dt>expressió</dt> + <dd>A partir d'ECMAScript 6, també es pot utilitzar expressions per a calcular el nom d'una propietat a lligar a la funció donada.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>A vegades és desitjable permetre l'accès a una propietat que retorna un valor calculat dinàmicament, o potser es vol reflectir l'estat d'una variable interna sense ésser necessari l'ús de crides explícites a mètodes. En JavaScript, Això es pot aconseguir utilitzant un <em>getter</em>. No és possible tenir simultàniament un lligam a una propietat i que aquesta mateixa propietat contingui un valor, tot i que sí és possible utilitzar un getter i un setter en conjunt per crear un tipus de pseudo-propietat.</p> + +<p>Tingueu en compte el següent quan treballeu amb la sintàxi <code>get</code>:</p> + +<div> +<ul> + <li>Pot tenir com a identificador tant un número com una string;</li> + <li>Ha de tenir exactament zero paràmetres (vegeu <a class="external" href="http://whereswalden.com/2010/08/22/incompatible-es5-change-literal-getter-and-setter-functions-must-now-have-exactly-zero-or-one-arguments/" rel="external nofollow">Canvi incompatible <abbr title="ECMAScript 5th edition">ES5</abbr>: les funcions getter i setter literals han de tenir zero o un argument</a> per més informació).</li> + <li>No pot aparèixer en un objecte literla amb un altre <code>get</code> o amb una entrada de dades per la mateixa propietat (<code>{ get x() { }, get x() { } }</code> i <code>{ x: ..., get x() { } }</code> estàn prohibits).</li> +</ul> +</div> + +<p>Un getter pot ser eliminiat utilitzant l'operador <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete" title="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/delete_Operator">delete.</a></code></p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Definir_un_getter_en_nous_objectes_en_inicialitzadors_d'objectes">Definir un getter en nous objectes, en inicialitzadors d'objectes</h3> + +<p>Això crearà una pseudo-propietat <code>latest</code> per l'objecte <code>obj</code>, el qual retornarà l'últim ítem de l'array en <code>log</code>.</p> + +<pre class="brush: js">var log = ['test']; +var obj = { + get latest () { + if (log.length == 0) retorna undefined; + return log[log.length - 1] + } +} +console.log (obj.latest); // Retornarà "test". +</pre> + +<p>Recordeu que intentar assignar un valor a <code>latest</code> no el canviarà.</p> + +<h3 id="Eliminar_un_getter_utilitzanr_l'operador_delete">Eliminar un getter utilitzanr l'operador <code>delete</code></h3> + +<p>Si voleu eliminar el getter, simplement utilitzeu <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/delete">delete</a></code>:</p> + +<pre class="brush: js">delete obj.latest; +</pre> + +<h3 id="Definir_un_getter_en_objectes_existents_utilitzant_la_Propietat_define">Definir un getter en objectes existents utilitzant la<code> Propietat</code> <code>define</code></h3> + +<p>Per annexar un getter a un objecte existent posteriorment en qualsevol moment, utilitzeu {{jsxref("Object.defineProperty()")}}.</p> + +<pre class="brush: js">var o = { a:0 } + +Object.defineProperty(o, "b", { get: function () { return this.a + 1; } }); + +console.log(o.b) // Executa el getter, el qual produeix a + 1 (que és 1)</pre> + +<h3 id="Utilitzar_un_nom_de_propietat_calculat">Utilitzar un nom de propietat calculat</h3> + +<div class="note"> +<p><strong>Nota:</strong> Les propietats calculades són una tecnologia experimental, que forma part de la proposta d'ECMAScript 6, i encara no estàn ampliament suportats pels navegadors. Això llençarà un error de sintàxi en entorns que no les suportin.</p> +</div> + +<pre class="brush: js">var expr = "foo"; + +var obj = { + get [expr]() { return "bar"; } +}; + +console.log(obj.foo); // "bar"</pre> + +<h3 id="Getters_smart_self-overwriting_lazy"> Getters smart / self-overwriting / lazy</h3> + +<p>Getters et donen una forma de definir una propietat d'un objecte, però aquests no calculen el valor de la propietat fins que no s'hi ha accedit. getter posposen el cost de calcular el valor fins el moment en que es necessiti el valor, i si no és mai necessari, mai es pagarà el cost.</p> + +<p>Una tècnica d'optimització addiccional per retardar o realitzar de forma lenta el càlcul del valor d'una propietat i guardar-ho per a accedir-hi posteriorment són els<strong> getters smart o<em> <a href="https://en.wikipedia.org/wiki/Memoization">memoized</a> </em></strong>. El valor és calculat el primer cop que es crida el getter, i és llavors guardat per tal els accessos subsegüents retornin el calor guardat en caché sense recalcular-lo. Això és útil en les situacions següents:</p> + +<ul> + <li>Si el càlcul del valor d'una propietat és car (utilitza massa RAM o massa temps de CPU, spawns worker thread, recupera els arxius remots, etc).</li> + <li>Si el valor no és necessari ara mateix. S'utilitzarà després, o en alguns casos no s'utilitzarà mai.</li> + <li>Si s'utilitza, serà accedit diverses vegades, i no hi ha necessitat de recalcular el valor que no es canviarà, o no s'hauria de recalcular.</li> +</ul> + +<p>Això vol dir que no haurieu d'utilitzar un getter lazy per una propietat el valor de la qual espereu canviar, ja que el getter no recalcularà el valor.</p> + +<p>En l'exemple següent, l'objecte té un getter com propietat pròpia. Obtenint la propietat, la propietat s'elimina de l'objecte i es reafegeix, però implícitament com a propietat de tipus data aquest cop. Finalment el valor es retorna.</p> + +<pre class="brush: js">get notifier() { + delete this.notifier; + return this.notifier = document.getElementById("bookmarked-notification-anchor"); +},</pre> + +<p>Per codi Firefox, vegeu també el mòdul de codi XPCOMUtils.jsm, el qual defineix la funció <code><a href="/en-US/docs/Mozilla/JavaScript_code_modules/XPCOMUtils.jsm#defineLazyGetter()">defineLazyGetter()</a></code>.</p> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-11.1.5', 'Object Initializer')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definició inicial.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-method-definitions', 'Method definitions')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Noms de propietats calculats afegits.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-method-definitions', 'Method definitions')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome(1)}}</td> + <td>{{ CompatGeckoDesktop("1.8.1") }}</td> + <td>{{ CompatIE(9) }}</td> + <td>9.5</td> + <td>3</td> + </tr> + <tr> + <td>Noms de propietats calculats</td> + <td>{{CompatChrome(46)}}</td> + <td>{{ CompatGeckoDesktop("34") }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{ CompatGeckoMobile("1.8.1") }}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Noms de propietats calculats</td> + <td>47</td> + <td>{{CompatNo}}</td> + <td>{{ CompatGeckoMobile("34.0") }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">setter</a></li> + <li>{{jsxref("Operators/delete", "delete")}}</li> + <li>{{jsxref("Object.defineProperty()")}}</li> + <li>{{jsxref("Object.defineGetter", "__defineGetter__")}}</li> + <li>{{jsxref("Object.defineSetter", "__defineSetter__")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#Defining_getters_and_setters">Definir Getters i Setters</a> en la guia de JavaScript</li> +</ul> diff --git a/files/ca/web/javascript/reference/functions/index.html b/files/ca/web/javascript/reference/functions/index.html new file mode 100644 index 0000000000..1dbb672ef5 --- /dev/null +++ b/files/ca/web/javascript/reference/functions/index.html @@ -0,0 +1,617 @@ +--- +title: Functions +slug: Web/JavaScript/Reference/Functions +tags: + - Function + - Functions + - JavaScript + - NeedsTranslation + - TopicStub +translation_of: Web/JavaScript/Reference/Functions +--- +<div>{{jsSidebar("Functions")}}</div> + +<p>Generally speaking, a function is a "subprogram" that can be <em>called</em> by code external (or internal in the case of recursion) to the function. Like the program itself, a function is composed of a sequence of statements called the <em>function body</em>. Values can be <em>passed</em> to a function, and the function can <em>return</em> a value.</p> + +<p>In JavaScript, functions are first-class objects, i.e. they are objects and can be manipulated and passed around just like any other object. Specifically, they are <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function">Function</a></code> objects.</p> + +<p>For more examples and explanations, see also the <a href="/en-US/docs/Web/JavaScript/Guide/Functions">JavaScript guide about functions</a>.</p> + +<h2 id="Description">Description</h2> + +<p>Every function in JavaScript is a <code>Function</code> object. See {{jsxref("Function")}} for information on properties and methods of <code>Function</code> objects.</p> + +<p>Functions are not the same as procedures. A function always returns a value, but a procedure may or may not return any value.</p> + +<p>To return a specific value other than the default, a function must have a <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/return">return</a></code> statement that specifies the value to return. A function without a return statement will return a default value. In the case of a <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor">constructor</a> called with the <code><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new">new</a></code> keyword, the default value is the value of its <code>this</code> parameter. For all other functions, the default return value is <code>undefined</code>.</p> + +<p>The parameters of a function call are the function's <em>arguments</em>. Arguments are passed to functions <em>by value</em>. If the function changes the value of an argument, this change is not reflected globally or in the calling function. However, object references are values, too, and they are special: if the function changes the referred object's properties, that change is visible outside the function, as shown in the following example:</p> + +<pre class="brush: js">/* Declare the function 'myFunc' */ +function myFunc(theObject) { + theObject.brand = "Toyota"; + } + + /* + * Declare variable 'mycar'; + * create and initialize a new Object; + * assign reference to it to 'mycar' + */ + var mycar = { + brand: "Honda", + model: "Accord", + year: 1998 + }; + + /* Logs 'Honda' */ + console.log(mycar.brand); + + /* Pass object reference to the function */ + myFunc(mycar); + + /* + * Logs 'Toyota' as the value of the 'brand' property + * of the object, as changed to by the function. + */ + console.log(mycar.brand); +</pre> + +<p>The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/this"><code>this</code> keyword</a> does not refer to the currently executing function, so you must refer to <code>Function</code> objects by name, even within the function body.</p> + +<h2 id="Defining_functions">Defining functions</h2> + +<p>There are several ways to define functions:</p> + +<h3 id="The_function_declaration_(function_statement)">The function declaration (<code>function</code> statement)</h3> + +<p>There is a special syntax for declaring functions (see <a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">function statement</a> for details):</p> + +<pre class="syntaxbox">function <em>name</em>([<em>param</em>[, <em>param</em>[, ... <em>param</em>]]]) { + <em>statements</em> +} +</pre> + +<dl> + <dt><code>name</code></dt> + <dd>The function name.</dd> +</dl> + +<dl> + <dt><code>param</code></dt> + <dd>The name of an argument to be passed to the function. A function can have up to 255 arguments.</dd> +</dl> + +<dl> + <dt><code>statements</code></dt> + <dd>The statements comprising the body of the function.</dd> +</dl> + +<h3 id="The_function_expression_(function_expression)">The function expression (<code>function</code> expression)</h3> + +<p>A function expression is similar to and has the same syntax as a function declaration (see <a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">function expression</a> for details):</p> + +<pre class="syntaxbox">function [<em>name</em>]([<em>param</em>] [, <em>param</em>] [..., <em>param</em>]) { + <em>statements</em> +} +</pre> + +<dl> + <dt><code>name</code></dt> + <dd>The function name. Can be omitted, in which case the function becomes known as an anonymous function.</dd> +</dl> + +<dl> + <dt><code>param</code></dt> + <dd>The name of an argument to be passed to the function. A function can have up to 255 arguments.</dd> + <dt><code>statements</code></dt> + <dd>The statements which comprise the body of the function.</dd> +</dl> + +<h3 id="The_generator_function_declaration_(function*_statement)">The generator function declaration (<code>function*</code> statement)</h3> + +<div class="note"> +<p><strong>Note:</strong> Generator function are an <em>experimental technology,</em> part of the ECMAScript 6 proposal, and are not widely supported by browsers yet.</p> +</div> + +<p>There is a special syntax for declaration generator functions (see {{jsxref('Statements/function*', 'function* statement')}} for details):</p> + +<pre class="syntaxbox">function* <em>name</em>([<em>param</em>[, <em>param</em>[, ... <em>param</em>]]]) { + <em>statements</em> +} +</pre> + +<dl> + <dt><code>name</code></dt> + <dd>The function name.</dd> +</dl> + +<dl> + <dt><code>param</code></dt> + <dd>The name of an argument to be passed to the function. A function can have up to 255 arguments.</dd> +</dl> + +<dl> + <dt><code>statements</code></dt> + <dd>The statements comprising the body of the function.</dd> +</dl> + +<h3 id="The_generator_function_expression_(function*_expression)">The generator function expression (<code>function*</code> expression)</h3> + +<div class="note"> +<p><strong>Note:</strong> Generator function are an <em>experimental technology,</em> part of the ECMAScript 6 proposal, and are not widely supported by browsers yet.</p> +</div> + +<p>A generator function expression is similar to and has the same syntax as a generator function declaration (see {{jsxref('Operators/function*', 'function* expression')}} for details):</p> + +<pre class="syntaxbox">function* [<em>name</em>]([<em>param</em>] [, <em>param</em>] [..., <em>param</em>]) { + <em>statements</em> +} +</pre> + +<dl> + <dt><code>name</code></dt> + <dd>The function name. Can be omitted, in which case the function becomes known as an anonymous function.</dd> +</dl> + +<dl> + <dt><code>param</code></dt> + <dd>The name of an argument to be passed to the function. A function can have up to 255 arguments.</dd> + <dt><code>statements</code></dt> + <dd>The statements which comprise the body of the function.</dd> +</dl> + +<h3 id="The_arrow_function_expression_(>)">The arrow function expression (=>)</h3> + +<div class="note"> +<p><strong>Note:</strong> Arrow function expressions are an <em>experimental technology,</em> part of the ECMAScript 6 proposal, and are not widely supported by browsers yet.</p> +</div> + +<p>An arrow function expression has a shorter syntax and lexically binds its this value (see <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">arrow functions</a> for details):</p> + +<pre class="syntaxbox">([param] [, param]) => { + statements +} + +param => expression +</pre> + +<dl> + <dt><code>param</code></dt> + <dd>The name of an argument. Zero arguments need to be indicated with <code>()</code>. For only one argument the parentheses are not required. (like <code>foo => 1</code>)</dd> + <dt><code>statements or expression</code></dt> + <dd>Multiple statements need to be enclosed in brackets. A single expression requires no brackets. The expression is also the implicit return value of that function.</dd> +</dl> + +<h3 id="The_Function_constructor">The <code>Function</code> constructor</h3> + +<div class="note"> +<p><strong>Note:</strong> Using the <code>Function</code> constructor to create functions is not recommended since it needs the function body as a string which may prevent some JS engine optimizations and can also cause other problems.</p> +</div> + +<p>As all other objects, {{jsxref("Function")}} objects can be created using the <code>new</code> operator:</p> + +<pre class="syntaxbox">new Function (<em>arg1</em>, <em>arg2</em>, ... <em>argN</em>, <em>functionBody</em>) +</pre> + +<dl> + <dt><code>arg1, arg2, ... arg<em>N</em></code></dt> + <dd>Zero or more names to be used by the function as formal argument names. Each must be a string that conforms to the rules for a valid JavaScript identifier or a list of such strings separated with a comma; for example "<code>x</code>", "<code>theValue</code>", or "<code>a,b</code>".</dd> +</dl> + +<dl> + <dt><code>functionBody</code></dt> + <dd>A string containing the JavaScript statements comprising the function definition.</dd> +</dl> + +<p>Invoking the <code>Function</code> constructor as a function (without using the <code>new</code> operator) has the same effect as invoking it as a constructor.</p> + +<h3 id="The_GeneratorFunction_constructor">The <code>GeneratorFunction</code> constructor</h3> + +<div class="note"> +<p><strong>Note:</strong> Arrow function expressions are an <em>experimental technology,</em> part of the ECMAScript 6 proposal, and are not widely supported by browsers yet.</p> +</div> + +<div class="note"> +<p><strong>Note:</strong> <code>GeneratorFunction</code> is not a global object, but could be obtained from generator function instance (see {{jsxref("GeneratorFunction")}} for more detail).</p> +</div> + +<div class="note"> +<p><strong>Note:</strong> Using the <code>GeneratorFunction</code> constructor to create functions is not recommended since it needs the function body as a string which may prevent some JS engine optimizations and can also cause other problems.</p> +</div> + +<p>As all other objects, {{jsxref("GeneratorFunction")}} objects can be created using the <code>new</code> operator:</p> + +<pre class="syntaxbox">new GeneratorFunction (<em>arg1</em>, <em>arg2</em>, ... <em>argN</em>, <em>functionBody</em>) +</pre> + +<dl> + <dt><code>arg1, arg2, ... arg<em>N</em></code></dt> + <dd>Zero or more names to be used by the function as formal argument names. Each must be a string that conforms to the rules for a valid JavaScript identifier or a list of such strings separated with a comma; for example "<code>x</code>", "<code>theValue</code>", or "<code>a,b</code>".</dd> +</dl> + +<dl> + <dt><code>functionBody</code></dt> + <dd>A string containing the JavaScript statements comprising the function definition.</dd> +</dl> + +<p>Invoking the <code>Function</code> constructor as a function (without using the <code>new</code> operator) has the same effect as invoking it as a constructor.</p> + +<h2 id="Function_parameters">Function parameters</h2> + +<div class="note"> +<p><strong>Note:</strong> Default and rest parameters are <em>experimental technology,</em> part of the ECMAScript 6 proposal, and are not widely supported by browsers yet.</p> +</div> + +<h3 id="Default_parameters">Default parameters</h3> + +<p>Default function parameters allow formal parameters to be initialized with default values if no value or <code>undefined</code> is passed. For more details, see<a href="/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters"> default parameters</a>.</p> + +<h3 id="Rest_parameters">Rest parameters</h3> + +<p>The rest parameter syntax allows to represent an indefinite number of arguments as an array. For more details, see <a href="/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">rest parameters</a>.</p> + +<h2 id="The_arguments_object">The <code>arguments</code> object</h2> + +<p>You can refer to a function's arguments within the function by using the <code>arguments</code> object. See <a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments">arguments</a>.</p> + +<ul> + <li><code><a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments">arguments</a></code>: An array-like object containing the arguments passed to the currently executing function.</li> + <li><code><a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee">arguments.callee</a></code> {{Deprecated_inline}}: The currently executing function.</li> + <li><code><a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/caller">arguments.caller</a></code> {{Obsolete_inline}} : The function that invoked the currently executing function.</li> + <li><code><a href="/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/length">arguments.length</a></code>: The number of arguments passed to the function.</li> +</ul> + +<h2 id="Defining_method_functions">Defining method functions</h2> + +<h3 id="Getter_and_setter_functions">Getter and setter functions</h3> + +<p>You can define getters (accessor methods) and setters (mutator methods) on any standard built-in object or user-defined object that supports the addition of new properties. The syntax for defining getters and setters uses the object literal syntax.</p> + +<dl> + <dt><a href="/en-US/docs/Web/JavaScript/Reference/Functions/get">get</a></dt> + <dd> + <p>Binds an object property to a function that will be called when that property is looked up.</p> + </dd> + <dt><a href="/en-US/docs/Web/JavaScript/Reference/Functions/set">set</a></dt> + <dd>Binds an object property to a function to be called when there is an attempt to set that property.</dd> +</dl> + +<h3 id="Method_definition_syntax">Method definition syntax</h3> + +<div class="note"> +<p><strong>Note:</strong> <em>Method definitions are experimental technology,</em> part of the ECMAScript 6 proposal, and are not widely supported by browsers yet.</p> +</div> + +<p>Starting with ECMAScript 6, you are able to define own methods in a shorter syntax, similar to the getters and setters. See <a href="/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions">method definitions</a> for more information.</p> + +<pre class="brush: js">var obj = { + foo() {}, + bar() {} +};</pre> + +<h2 id="Function_constructor_vs._function_declaration_vs._function_expression"><code>Function</code> constructor vs. function declaration vs. function expression</h2> + +<p>Compare the following:</p> + +<p>A function defined with the <code>Function</code> constructor assigned to the variable <code>multiply:</code></p> + +<pre class="brush: js">function multiply(x, y) { + return x * y; +} +</pre> + +<p>A <em>function expression</em> of an anonymous function assigned to the variable <code>multiply:</code></p> + +<pre class="brush: js">var multiply = function(x, y) { + return x * y; +}; +</pre> + +<p>A <em>function expression</em> of a function named <code>func_name</code> assigned to the variable <code>multiply:</code></p> + +<pre class="brush: js">var multiply = function func_name(x, y) { + return x * y; +}; +</pre> + +<h3 id="Differences">Differences</h3> + +<p>All do approximately the same thing, with a few subtle differences:</p> + +<p>There is a distinction between the function name and the variable the function is assigned to. The function name cannot be changed, while the variable the function is assigned to can be reassigned. The function name can be used only within the function's body. Attempting to use it outside the function's body results in an error (or <code>undefined</code> if the function name was previously declared via a <code>var</code> statement). For example:</p> + +<pre class="brush: js">var y = function x() {}; +alert(x); // throws an error +</pre> + +<p>The function name also appears when the function is serialized via <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/toString"><code>Function</code>'s toString method</a>.</p> + +<p>On the other hand, the variable the function is assigned to is limited only by its scope, which is guaranteed to include the scope where the function is declared in.</p> + +<p>As the 4th example shows, the function name can be different from the variable the function is assigned to. They have no relation to each other.A function declaration also creates a variable with the same name as the function name. Thus, unlike those defined by function expressions, functions defined by function declarations can be accessed by their name in the scope they were defined in:</p> + +<p>A function defined by '<code>new Function'</code> does not have a function name. However, in the <a href="/en-US/docs/Mozilla/Projects/SpiderMonkey">SpiderMonkey</a> JavaScript engine, the serialized form of the function shows as if it has the name "anonymous". For example, <code>alert(new Function())</code> outputs:</p> + +<pre class="brush: js">function anonymous() { +} +</pre> + +<p>Since the function actually does not have a name, <code>anonymous</code> is not a variable that can be accessed within the function. For example, the following would result in an error:</p> + +<pre class="brush: js">var foo = new Function("alert(anonymous);"); +foo(); +</pre> + +<p>Unlike functions defined by function expressions or by the <code>Function</code> constructor, a function defined by a function declaration can be used before the function declaration itself. For example:</p> + +<pre class="brush: js">foo(); // alerts FOO! +function foo() { + alert('FOO!'); +} +</pre> + +<p>A function defined by a function expression inherits the current scope. That is, the function forms a closure. On the other hand, a function defined by a <code>Function</code> constructor does not inherit any scope other than the global scope (which all functions inherit).</p> + +<p>Functions defined by function expressions and function declarations are parsed only once, while those defined by the <code>Function</code> constructor are not. That is, the function body string passed to the <code>Function</code> constructor must be parsed each and every time the constructor is called. Although a function expression creates a closure every time, the function body is not reparsed, so function expressions are still faster than "<code>new Function(...)</code>". Therefore the <code>Function</code> constructor should generally be avoided whenever possible.</p> + +<p>It should be noted, however, that function expressions and function declarations nested within the function generated by parsing a <code>Function constructor</code> 's string aren't parsed repeatedly. For example:</p> + +<pre class="brush: js">var foo = (new Function("var bar = \'FOO!\';\nreturn(function() {\n\talert(bar);\n});"))(); +foo(); // The segment "function() {\n\talert(bar);\n}" of the function body string is not re-parsed.</pre> + +<p>A function declaration is very easily (and often unintentionally) turned into a function expression. A function declaration ceases to be one when it either:</p> + +<ul> + <li>becomes part of an expression</li> + <li>is no longer a "source element" of a function or the script itself. A "source element" is a non-nested statement in the script or a function body:</li> +</ul> + +<pre class="brush: js">var x = 0; // source element +if (x == 0) { // source element + x = 10; // not a source element + function boo() {} // not a source element +} +function foo() { // source element + var y = 20; // source element + function bar() {} // source element + while (y == 10) { // source element + function blah() {} // not a source element + y++; // not a source element + } +} +</pre> + +<h3 id="Examples">Examples</h3> + +<pre class="brush: js">// function declaration +function foo() {} + +// function expression +(function bar() {}) + +// function expression +x = function hello() {} + + +if (x) { + // function expression + function world() {} +} + + +// function declaration +function a() { + // function declaration + function b() {} + if (0) { + // function expression + function c() {} + } +} +</pre> + +<h2 id="Conditionally_defining_a_function">Conditionally defining a function</h2> + +<p>Functions can be conditionally defined using either //function statements// (an allowed extension to the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">ECMA-262 Edition 3</a> standard) or the <code>Function</code> constructor. Please note that such <a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=609832">function statements are no longer allowed in ES5 strict</a>. Additionally, this feature does not work consistently cross-browser, so you should not rely on it.</p> + +<p>In the following script, the <code>zero</code> function is never defined and cannot be invoked, because '<code>if (0)</code>' evaluates its condition to false:</p> + +<pre class="brush: js">if (0) { + function zero() { + document.writeln("This is zero."); + } +} +</pre> + +<p>If the script is changed so that the condition becomes '<code>if (1)</code>', function <code>zero</code> is defined.</p> + +<p>Note: Although this kind of function looks like a function declaration, it is actually an expression (or statement), since it is nested within another statement. See differences between function declarations and function expressions.</p> + +<p>Note: Some JavaScript engines, not including <a href="/en-US/docs/SpiderMonkey">SpiderMonkey</a>, incorrectly treat any function expression with a name as a function definition. This would lead to <code>zero</code> being defined, even with the always-false <code>if</code> condition. A safer way to define functions conditionally is to define the function anonymously and assign it to a variable:</p> + +<pre class="brush: js">if (0) { + var zero = function() { + document.writeln("This is zero."); + } +} +</pre> + +<h2 id="Examples_2">Examples</h2> + +<h3 id="Returning_a_formatted_number">Returning a formatted number</h3> + +<p>The following function returns a string containing the formatted representation of a number padded with leading zeros.</p> + +<pre class="brush: js">// This function returns a string padded with leading zeros +function padZeros(num, totalLen) { + var numStr = num.toString(); // Initialize return value as string + var numZeros = totalLen - numStr.length; // Calculate no. of zeros + for (var i = 1; i <= numZeros; i++) { + numStr = "0" + numStr; + } + return numStr; +} +</pre> + +<p>The following statements call the padZeros function.</p> + +<pre class="brush: js">var result; +result = padZeros(42,4); // returns "0042" +result = padZeros(42,2); // returns "42" +result = padZeros(5,4); // returns "0005" +</pre> + +<h3 id="Determining_whether_a_function_exists">Determining whether a function exists</h3> + +<p>You can determine whether a function exists by using the <code>typeof</code> operator. In the following example, a test is peformed to determine if the <code>window</code> object has a property called <code>noFunc</code> that is a function. If so, it is used; otherwise some other action is taken.</p> + +<pre class="brush: js"> if ('function' == typeof window.noFunc) { + // use noFunc() + } else { + // do something else + } +</pre> + +<p>Note that in the <code>if</code> test, a reference to <code>noFunc</code> is used—there are no brackets "()" after the function name so the actual function is not called.</p> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Implemented in JavaScript 1.0</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-13', 'Function Definition')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>New: Arrow functions, Generator functions, default parameters, rest parameters</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#', 'function*')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-arrow-function-definitions', 'Arrow Function Definitions')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Generator function</td> + <td>39</td> + <td>{{CompatGeckoDesktop("26.0")}}</td> + <td>{{CompatUnknown}}</td> + <td>26</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>Arrow function</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop("22.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Generator function</td> + <td>{{CompatUnknown}}</td> + <td>39</td> + <td>{{CompatGeckoMobile("26.0")}}</td> + <td>{{CompatUnknown}}</td> + <td>26</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>Arrow function</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("22.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Statements/function", "function statement")}}</li> + <li>{{jsxref("Operators/function", "function expression")}}</li> + <li>{{jsxref("Statements/function*", "function* statement")}}</li> + <li>{{jsxref("Operators/function*", "function* expression")}}</li> + <li>{{jsxref("Function")}}</li> + <li>{{jsxref("GeneratorFunction")}}</li> + <li>{{jsxref("Functions/Arrow_functions", "Arrow functions")}}</li> + <li>{{jsxref("Functions/Default_parameters", "Default parameters")}}</li> + <li>{{jsxref("Functions/rest_parameters", "Rest parameters")}}</li> + <li>{{jsxref("Functions/arguments", "Arguments object")}}</li> + <li>{{jsxref("Functions/get", "getter")}}</li> + <li>{{jsxref("Functions/set", "setter")}}</li> + <li>{{jsxref("Functions/Method_definitions", "Method definitions")}}</li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope" title="JavaScript/Reference/Functions_and_function_scope">Functions and function scope</a></li> +</ul> diff --git a/files/ca/web/javascript/reference/functions/parameters_rest/index.html b/files/ca/web/javascript/reference/functions/parameters_rest/index.html new file mode 100644 index 0000000000..68fc5f0bba --- /dev/null +++ b/files/ca/web/javascript/reference/functions/parameters_rest/index.html @@ -0,0 +1,156 @@ +--- +title: Paràmetres rest +slug: Web/JavaScript/Reference/Functions/parameters_rest +translation_of: Web/JavaScript/Reference/Functions/rest_parameters +--- +<div>{{jsSidebar("Functions")}}</div> + +<p>El <strong>paràmetre rest</strong> ens permet representar un nombre indefinit d'arguments en forma d 'array.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="brush: js">function f(a, b, ...theArgs) { + // ... +} +</pre> + +<h2 id="Descripció">Descripció</h2> + +<p>Si l'últim argument d'una funció, està prefixat amb l'operador <code>...</code>, aquest esdevé un array el qual té com a elements des de <code>0</code> (inclòs) fins a <code>theArgs.length</code> (no inclòs) els arguments passats a la funció.</p> + +<p>A l'exemple de sobre, <code>theArgs</code> aglutina el tercer argument de la funció, ja que el primer està mapejat a <code>a</code> i el segon està mapejat a <code>b</code> , i tota la resta d'arguments consecutius. </p> + +<h3 id="Diferència_entre_els_paràmetres_rest_i_l'objecte_arguments">Diferència entre els paràmetres rest i l'objecte <code>arguments</code></h3> + +<p>Existeixen tres diferències principals entre els paràmetres rest i l'objecte <code>arguments</code></p> + +<ul> + <li>Els paràmetres rest són aquells als que no s' ha donat un nom per separat, mentre que l' objecte <code>arguments</code> conté tots els paràmetres passats a la funció</li> + <li>L' objecte <code>arguments</code> no és un array real, mentre que els paràmetres rest són instàncies d' <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" title="Array"><code>Array</code></a> , el qual significa que els hi podem aplicar directamaent els mètodes <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" title="Array sort method"><code>sort</code></a>, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" title="Array map method"><code>map</code></a>, <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach" title="Array forEach method"><code>forEach</code></a> o <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop" title="Array pop method">pop</a>.</code></li> + <li>L' objecte <code>arguments</code> té funcionalitat específica a sí mateix (com la propietat <code>calle</code>).</li> +</ul> + +<h3 id="Des_d'_arguments_fins_a_un_array">Des d' arguments fins a un array</h3> + +<p>El paràmetre rest ha estat introduit per tal de reduir la quantitat de codi utilitzat que es introduit per els arguments</p> + +<pre class="brush: js">// Anteriorment a l' existència del paràmetre rest el s' utilitzava seguent codi +function f(a, b) { + var args = Array.prototype.slice.call(arguments, f.length); + + // … +} + +// equivalent a + +function f(a, b, ...args) { + +} +</pre> + +<h3 id="Desestructurant_paràmetres_rest">Desestructurant paràmetres rest</h3> + +<p>Els paràmetres rest es poden desestructurar, que vol dir que els valors que contenen es poden desenpaquetar en variables diferents i separades. Vegeu <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">Destructuring assignment</a>.</p> + +<pre class="brush: js">function f(...[a, b, c]) { + return a + b + c; +} + +f(1) // NaN (bi c són undefined) +f(1, 2, 3) // 6 +f(1, 2, 3, 4) // 6 (el quart paràmetre no està desetructurat)</pre> + +<h2 id="Exemples">Exemples</h2> + +<p>Com que <code>theArgs</code> és un array, la propietat <code>length</code> en retorna el recompte:</p> + +<pre class="brush: js">function fun1(...theArgs) { + console.log(theArgs.length); +} + +fun1(); // 0 +fun1(5); // 1 +fun1(5, 6, 7); // 3 +</pre> + +<p>En el seguent exemple el paràmetre rest s' utilitza per aglutinar tots els paràmetres passats a la funcció després del primer en un array. Després cadascun d' ells és multiplicat per el primer paràmetre passat a la funció i es retorna l' array.</p> + +<pre class="brush: js">function multiply(multiplier, ...theArgs) { + return theArgs.map(function(element) { + return multiplier * element; + }); +} + +var arr = multiply(2, 1, 2, 3); +console.log(arr); // [2, 4, 6] +</pre> + +<p>El seguent exemple mostra com els mètodes d' <code>Array</code> es poden utilitzar am paràmetres rest, però no amb l' objecte <code>arguments</code> :</p> + +<pre class="brush: js">function sortRestArgs(...theArgs) { + var sortedArgs = theArgs.sort(); + return sortedArgs; +} + +console.log(sortRestArgs(5, 3, 7, 1)); // mostra 1, 3, 5, 7 + +function sortArguments() { + var sortedArgs = arguments.sort(); + return sortedArgs; // aquesta línia mai s' executarà +} + +// genera el TypeError: arguments.sort no és una funció +console.log(sortArguments(5, 3, 7, 1)); +</pre> + +<p>Per tal de poder usar els mètodes d' <code>Array</code> amb l' objecte <code>arguments</code> , aquest s' ha de convertir primer en un array.</p> + +<pre class="brush: js">function sortArguments() { + var args = Array.prototype.slice.call(arguments); + var sortedArgs = args.sort(); + return sortedArgs; +} +console.log(sortArguments(5, 3, 7, 1)); // shows 1, 3, 5, 7 +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-function-definitions', 'Function Definitions')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-function-definitions', 'Function Definitions')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("javascript.functions.rest_parameters")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator" title="spread operator">Spread operator</a> (also ‘<code>...</code>’)</li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments" title="arguments">Arguments object</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" title="Array">Array</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions" title="Functions and function scope">Functions</a></li> + <li><a class="external" href="http://wiki.ecmascript.org/doku.php?id=harmony:rest_parameters">Original proposal at ecmascript.org</a></li> + <li><a class="external" href="http://javascriptweblog.wordpress.com/2011/01/18/javascripts-arguments-object-and-beyond/">JavaScript arguments object and beyond</a></li> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">Destructuring assignment</a></li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/dataview/buffer/index.html b/files/ca/web/javascript/reference/global_objects/dataview/buffer/index.html new file mode 100644 index 0000000000..40da57fa52 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/dataview/buffer/index.html @@ -0,0 +1,101 @@ +--- +title: DataView.prototype.buffer +slug: Web/JavaScript/Reference/Global_Objects/DataView/buffer +translation_of: Web/JavaScript/Reference/Global_Objects/DataView/buffer +--- +<div>{{JSRef}}</div> + +<p>La propietat d'accés <strong><code>buffer</code></strong> representa el {{jsxref("ArrayBuffer")}} referenciat per <code>DataView</code> en el temps de construcció.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><var>dataview</var>.buffer</pre> + +<h2 id="Descripció">Descripció</h2> + +<p>La propietat <code>buffer</code> és una propietat d'accés de la qual la seva funció d'accés és <code>undefined</code>, el que vol dir que només es pot llegir aquesta propietat. El valor s'estableix quan <code>DataView</code> és construït i no es pot canviar.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_la_propietat_buffer">Utilitzar la propietat <code>buffer</code></h3> + +<pre class="brush:js">var buffer = new ArrayBuffer(8); +var dataview = new DataView(buffer); +dataview.buffer; // ArrayBuffer { byteLength: 8 } +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-get-dataview.prototype.buffer', 'DataView.prototype.buffer')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>9.0</td> + <td>{{ CompatGeckoDesktop("15.0") }}</td> + <td>10</td> + <td>12.1</td> + <td>5.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>4.0</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{ CompatGeckoMobile("15") }}</td> + <td>{{CompatUnknown}}</td> + <td>12.0</td> + <td>4.2</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("DataView")}}</li> + <li>{{jsxref("ArrayBuffer")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/dataview/getfloat32/index.html b/files/ca/web/javascript/reference/global_objects/dataview/getfloat32/index.html new file mode 100644 index 0000000000..83de1562e4 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/dataview/getfloat32/index.html @@ -0,0 +1,122 @@ +--- +title: DataView.prototype.getFloat32() +slug: Web/JavaScript/Reference/Global_Objects/DataView/getFloat32 +translation_of: Web/JavaScript/Reference/Global_Objects/DataView/getFloat32 +--- +<div>{{JSRef}}</div> + +<p>El mètode <code>getFloat32()</code> obtè un nombre decimal en coma flotant amb signe de 32 bits (float) a la posició en bytes especificada, content des del començament de {{jsxref("DataView")}}.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><var>dataview</var>.getFloat32(byteOffset [, littleEndian])</pre> + +<h2 id="Paràmetres">Paràmetres</h2> + +<dl> + <dt>byteOffset</dt> + <dd>La posició, en bytes, des de l'inici de la vista a la que es llegiran les dades.</dd> + <dt>littleEndian</dt> + <dd>{{optional_inline}} Indica si el nombre decimal en coma flotant de 32 bits es troba emmagatzemat en format {{Glossary("Endianness", "little- o big-endian")}}. De ésser false o undefined, s'interpreta el valor com a big-endian.</dd> +</dl> + +<h3 id="Errors_llençats">Errors llençats</h3> + +<dl> + <dt>{{jsxref("RangeError")}}</dt> + <dd>Es llença si <code>byteOffset</code> té un valor que representa una posició més enllà del final de la vista.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>No hi ha cap restricció d'alineament; es poden llegir valors de múltiples bytes des de qualsevol posició.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_getFloat32">Utilitzar el mètode <code>getFloat32</code></h3> + +<pre class="brush:js">var buffer = new ArrayBuffer(8); +var dataview = new DataView(buffer); +dataview.getFloat32(1); // 0 +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('Typed Array')}}</td> + <td>{{Spec2('Typed Array')}}</td> + <td>Reemplaçada per ECMAScript 6.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-dataview.prototype.getfloat32', 'DataView.prototype.getFloat32')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial en un estàndard ECMA.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>9.0</td> + <td>{{CompatGeckoDesktop("15.0")}}</td> + <td>10</td> + <td>12.1</td> + <td>5.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>4.0</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{ CompatGeckoMobile("15") }}</td> + <td>{{CompatUnknown}}</td> + <td>12.0</td> + <td>4.2</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("DataView")}}</li> + <li>{{jsxref("ArrayBuffer")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/dataview/index.html b/files/ca/web/javascript/reference/global_objects/dataview/index.html new file mode 100644 index 0000000000..90c9d640ee --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/dataview/index.html @@ -0,0 +1,174 @@ +--- +title: DataView +slug: Web/JavaScript/Reference/Global_Objects/DataView +tags: + - Constructor + - DataView + - JavaScript + - NeedsTranslation + - TopicStub + - TypedArrays +translation_of: Web/JavaScript/Reference/Global_Objects/DataView +--- +<div>{{JSRef}}</div> + +<p>The <strong><code>DataView</code></strong> view provides a low-level interface for reading data from and writing it to an {{jsxref("ArrayBuffer")}}.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox">new DataView(buffer [, byteOffset [, byteLength]])</pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt><code>buffer</code></dt> + <dd>An existing {{jsxref("ArrayBuffer")}} to use as the storage for the new <code>DataView</code> object.</dd> + <dt><code>byteOffset</code> {{optional_inline}}</dt> + <dd>The offset, in bytes, to the first byte in the specified buffer for the new view to reference. If not specified, the view of the buffer will start with the first byte.</dd> + <dt><code>byteLength</code> {{optional_inline}}</dt> + <dd>The number of elements in the byte array. If unspecified, length of the view will match the buffer's length.</dd> +</dl> + +<h3 id="Return_value">Return value</h3> + +<p>A new <code>DataView</code> object representing the specified data buffer.</p> + +<h3 id="Errors_thrown">Errors thrown</h3> + +<dl> + <dt><code>{{jsxref("RangeError")}}</code></dt> + <dd>Thrown if the <code>byteOffset</code> and <code>byteLength</code> result in the specified view extending past the end of the buffer.</dd> +</dl> + +<h2 id="Description">Description</h2> + +<h3 id="Detect_endianness">Detect endianness</h3> + +<p>You'll probably need to detect the type of architecture your script is running, here is a little trick to check it. See {{Glossary("Endianness")}} for more information.</p> + +<pre class="brush: js">var littleEndian = (function() { + var buffer = new ArrayBuffer(2); + new DataView(buffer).setInt16(0, 256, true); + return new Int16Array(buffer)[0] === 256; +})(); +console.log(littleEndian); // true or false +</pre> + +<h2 id="Properties">Properties</h2> + +<dl> + <dt>DataView.length</dt> + <dd>The <code>DataView</code> constructor's length property whose value is 3.</dd> + <dt>{{jsxref("DataView.prototype")}}</dt> + <dd>Allows the addition of properties to all <code>DataView</code> objects.</dd> +</dl> + +<h2 id="DataView_instances"><code>DataView</code> instances</h2> + +<p>All <code>DataView</code> instances inherit from {{jsxref("DataView.prototype")}}.</p> + +<h3 id="Properties_2">Properties</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/DataView/prototype','Properties')}}</p> + +<h3 id="Methods">Methods</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/DataView/prototype','Methods')}}</p> + +<h2 id="Example">Example</h2> + +<pre class="brush: js">var buffer = new ArrayBuffer(16); +var dv = new DataView(buffer, 0); + +dv.setInt16(1, 42); +dv.getInt16(1); //42 +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-dataview-constructor', 'DataView')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition in an ECMA standard</td> + </tr> + <tr> + <td>{{SpecName('Typed Array')}}</td> + <td>{{Spec2('Typed Array')}}</td> + <td>Superseded by ECMAScript 6</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>9.0</td> + <td>{{CompatGeckoDesktop("15.0")}}</td> + <td>10</td> + <td>12.1</td> + <td>5.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>4.0</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("15")}}</td> + <td>{{CompatUnknown}}</td> + <td>12.0</td> + <td>4.2</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Firefox-specific_notes">Firefox-specific notes</h2> + +<p>Starting with Gecko / SpiderMonkey 40 {{geckoRelease(40)}}, <code>DataView</code> requires to be constructed with a {{jsxref("Operators/new", "new")}} operator. Calling <code>DataView()</code> as a function without <code>new</code>, will throw a {{jsxref("TypeError")}} from now on.</p> + +<pre class="brush: js example-bad">var dv = DataView(buffer, 0); +// TypeError: calling a builtin DataView constructor without new is forbidden</pre> + +<pre class="brush: js example-good">var dv = new DataView(buffer, 0);</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a class="link-https" href="https://github.com/jDataView/jDataView">jDataView</a>: JavaScript library that polyfills and extends the <code>DataView</code> API to all browsers and Node.js.</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/dataview/prototype/index.html b/files/ca/web/javascript/reference/global_objects/dataview/prototype/index.html new file mode 100644 index 0000000000..203e5076e4 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/dataview/prototype/index.html @@ -0,0 +1,146 @@ +--- +title: DataView.prototype +slug: Web/JavaScript/Reference/Global_Objects/DataView/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/DataView +--- +<div>{{JSRef}}</div> + +<p>La propietat <code><strong>DataView</strong></code><strong><code>.prototype</code></strong> representa el prototip de l'objecte {{jsxref("DataView")}}.</p> + +<div>{{js_property_attributes(0,0,0)}}</div> + +<h2 id="Descripció">Descripció</h2> + +<p>Les instàncies <code>DataView</code> hereten de <code>DataView.prototype</code>. Com passa amb tots els constructors, podeu canviar l'objecte prototip del constructor per produir canvis a totes les instàncies <code>DataView</code>.</p> + +<h2 id="Propietats">Propietats</h2> + +<dl> + <dt>DataView.prototype.constructor</dt> + <dd>Especifica la funció que crea un prototip de l'objecte. El valor inicial és el constructor integrat estàndard <code>DataView</code>.</dd> + <dt>{{jsxref("DataView.prototype.buffer")}} {{readonlyInline}}</dt> + <dd>L'{{jsxref("ArrayBuffer")}} referenciat per aquesta vista. Fixat en el temps de construcció i per tant és <strong>només de lectura.</strong></dd> + <dt>{{jsxref("DataView.prototype.byteLength")}} {{readonlyInline}}</dt> + <dd>La llargària (en bytes) d'aquesta vista des del començament del seu {{jsxref("ArrayBuffer")}}. Fixat en el temps de construcció i per tant és <strong>només de lectura.</strong></dd> + <dt>{{jsxref("DataView.prototype.byteOffset")}} {{readonlyInline}}</dt> + <dd>La posició (en bytes) d'aquesta vista des de l'inici del seu {{jsxref("ArrayBuffer")}}. Fixat en el temps de construcció i per tant és <strong>només de lectura.</strong></dd> +</dl> + +<h2 id="Mètodes">Mètodes</h2> + +<h3 id="Lectura">Lectura</h3> + +<dl> + <dt>{{jsxref("DataView.prototype.getInt8()")}}</dt> + <dd>Obté un nombre sencer (byte) de 8 bits amb signe al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.getUint8()")}}</dt> + <dd>Obté un nombre sencer sense signe de 8 bits (unsigned byte) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.getInt16()")}}</dt> + <dd>Obté un nombre sencer de 16 bits (short) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.getUint16()")}}</dt> + <dd>Obté un nombre sencer sense signe de 16 bits (unsigned short) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.getInt32()")}}</dt> + <dd>Obté un nombre sencer de 32 bits (long) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.getUint32()")}}</dt> + <dd>Obté un nombre sencer sense signe de 31 bits (unsigned long) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.getFloat32()")}}</dt> + <dd>Obté un nombre en coma flotant amb signe de 32 bits (float) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.getFloat64()")}}</dt> + <dd>Obté un nombre en coma flotant amb signe de 64 bits (double) al byte de posició especificat des de l'inici de la vista.</dd> +</dl> + +<h3 id="Escritura">Escritura</h3> + +<dl> + <dt>{{jsxref("DataView.prototype.setInt8()")}}</dt> + <dd>Emmagatzema el valor d'un nombre sencer de 8 bits (byte) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.setUint8()")}}</dt> + <dd>Emmagatzema el valor d'un nombre sencer sense signe de 8 bits (unsigned byte) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.setInt16()")}}</dt> + <dd>Emmagatzema el valor d'un nombre sencer amb signe de 16 bits (short) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.setUint16()")}}</dt> + <dd>Emmagatzema el valor d'un nombre sencer sense signe de 16 bits (unsigned short) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.setInt32()")}}</dt> + <dd>Emmagatzema el valor d'un nombre sencer amb signe de 32 bits (long) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.setUint32()")}}</dt> + <dd>Emmagatzema el valor d'un nombre sencer sense signe de 32 bits (unsigned long) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.setFloat32()")}}</dt> + <dd>Emmagatzema el valor d'un nombre en coma flotant amb signe de 32 bits (float) al byte de posició especificat des de l'inici de la vista.</dd> + <dt>{{jsxref("DataView.prototype.setFloat64()")}}</dt> + <dd>Emmagatzema el valor d'un nombre en coma flotant amb signe de 64 bits (double) al byte de posició especificat des de l'inici de la vista.</dd> +</dl> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-dataview.prototype', 'DataView.prototype')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegador">Compatibilitat amb navegador</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>9.0</td> + <td>{{ CompatGeckoDesktop("15.0") }}</td> + <td>10</td> + <td>12.1</td> + <td>5.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>4.0</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("15")}}</td> + <td>{{CompatUnknown}}</td> + <td>12.0</td> + <td>4.2</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("DataView")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/evalerror/index.html b/files/ca/web/javascript/reference/global_objects/evalerror/index.html new file mode 100644 index 0000000000..65a4df349e --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/evalerror/index.html @@ -0,0 +1,161 @@ +--- +title: EvalError +slug: Web/JavaScript/Reference/Global_Objects/EvalError +tags: + - Error + - EvalError + - JavaScript + - NeedsTranslation + - Reference + - TopicStub +translation_of: Web/JavaScript/Reference/Global_Objects/EvalError +--- +<div>{{JSRef}}</div> + +<p>The <strong><code>EvalError</code></strong> object indicates an error regarding the global {{jsxref("Global_Objects/eval", "eval()")}} function.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><code>new EvalError([<var>message</var>[, <var>fileName</var>[, <var>lineNumber</var>]]])</code></pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt><code>message</code></dt> + <dd>Optional. Human-readable description of the error</dd> + <dt><code>fileName</code> {{non-standard_inline}}</dt> + <dd>Optional. The name of the file containing the code that caused the exception</dd> + <dt><code>lineNumber</code> {{non-standard_inline}}</dt> + <dd>Optional. The line number of the code that caused the exception</dd> +</dl> + +<h2 id="Description">Description</h2> + +<p>An <code>EvalError</code> is thrown when the global {{jsxref("Global_Objects/eval", "eval()")}} function is used improperly.</p> + +<h2 id="Properties">Properties</h2> + +<dl> + <dt>{{jsxref("EvalError.prototype")}}</dt> + <dd>Allows the addition of properties to an <code>EvalError</code> object.</dd> +</dl> + +<h2 id="Methods">Methods</h2> + +<p>The global <code>EvalError</code> contains no methods of its own, however, it does inherit some methods through the prototype chain.</p> + +<h2 id="EvalError_instances"><code>EvalError</code> instances</h2> + +<h3 id="Properties_2">Properties</h3> + +<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError/prototype', 'Properties')}}</div> + +<h3 id="Methods_2">Methods</h3> + +<div>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError/prototype', 'Methods')}}</div> + +<h2 id="Examples">Examples</h2> + +<p><code>EvalError</code> is not used now, and never be thrown by the runtime.</p> + +<h3 id="Creating_an_EvalError">Creating an <code>EvalError</code></h3> + +<pre class="brush: js">try { + throw new EvalError('Hello', 'someFile.js', 10); +} catch (e) { + console.log(e instanceof EvalError); // true + console.log(e.message); // "Hello" + console.log(e.name); // "EvalError" + console.log(e.fileName); // "someFile.js" + console.log(e.lineNumber); // 10 + console.log(e.columnNumber); // 0 + console.log(e.stack); // "@Scratchpad/2:2:9\n" +} +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.11.6.1', 'EvalError')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Not used in this specification. Present for backward compatibility.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-native-error-types-used-in-this-standard-evalerror', 'EvalError')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Not used in this specification. Present for backward compatibility.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Error")}}</li> + <li>{{jsxref("EvalError.prototype")}}</li> + <li>{{jsxref("Global_Objects/eval", "eval()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/evalerror/prototype/index.html b/files/ca/web/javascript/reference/global_objects/evalerror/prototype/index.html new file mode 100644 index 0000000000..8aecc42c15 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/evalerror/prototype/index.html @@ -0,0 +1,123 @@ +--- +title: EvalError.prototype +slug: Web/JavaScript/Reference/Global_Objects/EvalError/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/EvalError +--- +<div>{{JSRef}}</div> + +<p>La propietat <code><strong>EvalError.prototype</strong></code> representa el prototip del constructor {{jsxref("EvalError")}}.</p> + +<div>{{js_property_attributes(0, 0, 0)}}</div> + +<h2 id="Descripció">Descripció</h2> + +<p>Totes les instàncies {{jsxref("EvalError")}} hereten de <code>EvalError.prototype</code>. Es pot utilitzar el prototip per afegir propietats o mètodes a totes les instàncies.</p> + +<h2 id="Propietats">Propietats</h2> + +<dl> + <dt><code>EvalError.prototype.constructor</code></dt> + <dd>Especifica la funció que ha creat el prototip d'una instància.</dd> + <dt>{{jsxref("Error.prototype.message", "EvalError.prototype.message")}}</dt> + <dd>Missatge d'error. Tot i que l'ECMA-262 especifica que {{jsxref("EvalError")}} hauria de proveir la seva pròpia propietat <code>message</code>, en <a href="/en-US/docs/Mozilla/Projects/SpiderMonkey">SpiderMonkey</a>, hereta {{jsxref("Error.prototype.message")}}.</dd> + <dt>{{jsxref("Error.prototype.name", "EvalError.prototype.name")}}</dt> + <dd>Nom de l'error. Heretat de {{jsxref("Error")}}.</dd> + <dt>{{jsxref("Error.prototype.fileName", "EvalError.prototype.fileName")}}</dt> + <dd>Camí cap al fitxer que ha llançat aquest error. Heretat de {{jsxref("Error")}}.</dd> + <dt>{{jsxref("Error.prototype.lineNumber", "EvalError.prototype.lineNumber")}}</dt> + <dd>Número de línia en el fitxer que ha llançat aquest error. Heretat de {{jsxref("Error")}}.</dd> + <dt>{{jsxref("Error.prototype.columnNumber", "EvalError.prototype.columnNumber")}}</dt> + <dd>Número de columna en la línia que ha llançat aquest error. Heretat de {{jsxref("Error")}}.</dd> + <dt>{{jsxref("Error.prototype.stack", "EvalError.prototype.stack")}}</dt> + <dd>Traça de l'error. Heretat de {{jsxref("Error")}}.</dd> +</dl> + +<h2 id="Mètodes">Mètodes</h2> + +<p>Tot i que l'objecte prototip {{jsxref("EvalError")}} no contè cap mètode en si mateix, les instàncies {{jsxref("EvalError")}} hereten alguns mètodes a través de la cadena prototip.</p> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES3')}}</td> + <td>{{Spec2('ES3')}}</td> + <td>Definició inicial</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.11.7.6', 'NativeError.prototype')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definit com a <code><em>NativeError</em>.prototype</code>.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-nativeerror.prototype', 'NativeError.prototype')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definit com a <code><em>NativeError</em>.prototype</code>.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Error.prototype")}}</li> + <li>{{jsxref("Function.prototype")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/function/arguments/index.html b/files/ca/web/javascript/reference/global_objects/function/arguments/index.html new file mode 100644 index 0000000000..6371caeb23 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/function/arguments/index.html @@ -0,0 +1,125 @@ +--- +title: Function.arguments +slug: Web/JavaScript/Reference/Global_Objects/Function/arguments +translation_of: Web/JavaScript/Reference/Global_Objects/Function/arguments +--- +<div>{{JSRef}} {{deprecated_header}}</div> + +<p>La propietat <code><strong><em>function</em>.arguments</strong></code> es refereix a un objecte que s'assembla a una array corresponent als arguments passats a una funció. Utilitzeu la variable simple {{jsxref("Functions/arguments", "arguments")}}.</p> + +<h2 id="Descripció">Descripció</h2> + +<p>La sintaxis <code><em>function</em>.arguments</code> és obsoleta. El camí recomanat per accedir a l'objecte {{jsxref("Functions/arguments", "arguments")}} disponible en les funcions requereix simplement referir-se a la variable{{jsxref("Functions/arguments", "arguments")}}.</p> + +<p>En cas de recursivitat, és a dir, si la funció <code>f</code> apareix vàries vegades en la pila de crides, el valor det <code>f.arguments</code> representa els arguments corresponents a la invocació més recent de la funció.</p> + +<p>El valor del la propietat arguments normalment és null si no hi ha una invocació excel·lent de la funció (això vol dir, que s'ha cridat la funció però la seva execució encara no s'ha acabat).</p> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">function f(n) { g(n - 1); } + +function g(n) { + console.log('before: ' + g.arguments[0]); + if (n > 0) { f(n); } + console.log('after: ' + g.arguments[0]); +} + +f(2); + +console.log('returned: ' + g.arguments); + +// Resultat + +// abans: 1 +// abans: 0 +// després: 0 +// després: 1 +// retorn: null +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Definició inicial. Implementat en JavaScript 1.0. Obsolet a favor de {{jsxref("Functions/arguments", "arguments")}} en ES3.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-10.6', 'arguments object')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>{{jsxref("Functions/arguments", "arguments")}} object</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-arguments-object', 'arguments object')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>{{jsxref("Functions/arguments", "arguments")}} object</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característca</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>Objecte {{jsxref("Functions/arguments", "arguments")}}</li> + <li>{{jsxref("Functions", "Functions and function scope", "", 1)}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/function/arity/index.html b/files/ca/web/javascript/reference/global_objects/function/arity/index.html new file mode 100644 index 0000000000..d330307535 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/function/arity/index.html @@ -0,0 +1,70 @@ +--- +title: Function.arity +slug: Web/JavaScript/Reference/Global_Objects/Function/arity +translation_of: Archive/Web/JavaScript/Function.arity +--- +<div>{{JSRef}} {{obsolete_header}}</div> + +<p class="note">La propietat <code><strong>arity</strong></code> solia retornar el número d'arguments esperats per la funció, tanmateix, ja no existeix i s'ha reemplaçat per la propietat {{jsxref("Function.prototype.length")}}.</p> + +<h2 id="Especificacions">Especificacions</h2> + +<p>Implementat en JavaScript 1.2. Obsolet en JavaScript 1.4.}</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Function.prototype.length")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/function/caller/index.html b/files/ca/web/javascript/reference/global_objects/function/caller/index.html new file mode 100644 index 0000000000..613e163d6a --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/function/caller/index.html @@ -0,0 +1,124 @@ +--- +title: Function.caller +slug: Web/JavaScript/Reference/Global_Objects/Function/caller +translation_of: Web/JavaScript/Reference/Global_Objects/Function/caller +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>La propietat <code><strong><em>function</em>.caller</strong></code> retorna la funció que ha invocat la funció especificada.</p> + +<h2 id="Descripció">Descripció</h2> + +<p>SI la funció <code>f</code> ha estat invocada pel codi de nivell superior, el valor de <code>f.caller</code> és {{jsxref("null")}}, sinó és la funció que ha cridat <code>f</code>.</p> + +<p>Aquesta propietat reemplaça la propietat obsoleta {{jsxref("Functions/arguments/caller", "arguments.caller")}} de l'objecte {{jsxref("Functions/arguments", "arguments")}}.</p> + +<p>La propietat especial <code>__caller__</code>, la qual retorna l'objecte activatwhich returned the activation object of the caller thus allowing to reconstruct the stack, was removed for security reasons.</p> + +<h3 id="Notes">Notes</h3> + +<p>Vegeu que en cas de recursió, no podeu reconstruir la pila de crida fent servir aquesta propietat. C that in case of recursion, you can't reconstruct the call stack using this property. Tingueu en compte:</p> + +<pre class="brush: js">function f(n) { g(n - 1); } +function g(n) { if (n > 0) { f(n); } else { stop(); } } +f(2); +</pre> + +<p>At the moment <code>stop()</code> is called the call stack will be:</p> + +<pre class="brush: js">f(2) -> g(1) -> f(1) -> g(0) -> stop() +</pre> + +<p>El següent és cert:</p> + +<pre class="brush: js">stop.caller === g && f.caller === g && g.caller === f +</pre> + +<p>so if you tried to get the stack trace in the <code>stop()</code> function like this:</p> + +<pre class="brush: js">var f = stop; +var stack = 'Stack trace:'; +while (f) { + stack += '\n' + f.name; + f = f.caller; +} +</pre> + +<p>El bucle mai s'aturaria.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Checking_the_value_of_a_function's_caller_property">Checking the value of a function's <code>caller</code> property</h3> + +<p>El codi següent comprova que el valor following code checks the value a function's <code>caller</code> property.</p> + +<pre class="brush: js">function myFunc() { + if (myFunc.caller == null) { + return 'The function was called from the top!'; + } else { + return 'This function\'s caller was ' + myFunc.caller; + } +} +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<p>No forma part de cap especificació. Implementat en JavaScript 1.5.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("1.0")}}</td> + <td>8.0</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("1.0")}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>Error d'Implementació de SpiderMonkey {{bug(65683)}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/function/index.html b/files/ca/web/javascript/reference/global_objects/function/index.html new file mode 100644 index 0000000000..9cb0571d13 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/function/index.html @@ -0,0 +1,236 @@ +--- +title: Function +slug: Web/JavaScript/Reference/Global_Objects/Function +tags: + - Constructor + - Function + - JavaScript + - NeedsTranslation + - TopicStub +translation_of: Web/JavaScript/Reference/Global_Objects/Function +--- +<div>{{JSRef}}</div> + +<p>The <strong><code>Function</code> constructor</strong> creates a new <code>Function</code> object. In JavaScript every function is actually a <code>Function</code> object.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><code>new Function ([<var>arg1</var>[, <var>arg2</var>[, ...<var>argN</var>]],] <var>functionBody</var>)</code></pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt><code>arg1, arg2, ... arg<em>N</em></code></dt> + <dd>Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "<code>x</code>", "<code>theValue</code>", or "<code>a,b</code>".</dd> + <dt><code>functionBody</code></dt> + <dd>A string containing the JavaScript statements comprising the function definition.</dd> +</dl> + +<h2 id="Description">Description</h2> + +<p><code>Function</code> objects created with the <code>Function</code> constructor are parsed when the function is created. This is less efficient than declaring a function with a <a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">function expression</a> or <a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">function statement</a> and calling it within your code, because such functions are parsed with the rest of the code.</p> + +<p>All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.</p> + +<div class="note"> +<p><strong>Note:</strong> Functions created with the <code>Function</code> constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the <code>Function</code> constructor was called. This is different from using {{jsxref("eval")}} with code for a function expression.</p> +</div> + +<p>Invoking the <code>Function</code> constructor as a function (without using the <code>new</code> operator) has the same effect as invoking it as a constructor.</p> + +<h2 id="Properties_and_Methods_of_Function">Properties and Methods of <code>Function</code></h2> + +<p>The global <code>Function</code> object has no methods or properties of its own, however, since it is a function itself it does inherit some methods and properties through the prototype chain from {{jsxref("Function.prototype")}}.</p> + +<h2 id="Function_prototype_object"><code>Function</code> prototype object</h2> + +<h3 id="Properties">Properties</h3> + +<div>{{page('/en-US/docs/JavaScript/Reference/Global_Objects/Function/prototype', 'Properties')}}</div> + +<h3 id="Methods">Methods</h3> + +<div>{{page('/en-US/docs/JavaScript/Reference/Global_Objects/Function/prototype', 'Methods')}}</div> + +<h2 id="Function_instances"><code>Function</code> instances</h2> + +<p><code>Function</code> instances inherit methods and properties from {{jsxref("Function.prototype")}}. As with all constructors, you can change the constructor's prototype object to make changes to all <code>Function</code> instances.</p> + +<h2 id="Examples">Examples</h2> + +<h3 id="Specifying_arguments_with_the_Function_constructor">Specifying arguments with the <code>Function</code> constructor</h3> + +<p>The following code creates a <code>Function</code> object that takes two arguments.</p> + +<pre class="brush: js">// Example can be run directly in your JavaScript console + +// Create a function that takes two arguments and returns the sum of those arguments +var adder = new Function('a', 'b', 'return a + b'); + +// Call the function +adder(2, 6); +// > 8 +</pre> + +<p>The arguments "<code>a</code>" and "<code>b</code>" are formal argument names that are used in the function body, "<code>return a + b</code>".</p> + +<h3 id="A_recursive_shortcut_to_massively_modify_the_DOM">A recursive shortcut to massively modify the DOM</h3> + +<p>Creating functions with the <code>Function</code> constructor is one of the ways to dynamically create an indeterminate number of new objects with some executable code into the global scope from a function. The following example (a recursive shortcut to massively modify the DOM) is impossible without the invocation of the <code>Function</code> constructor for each new query if you want to avoid closures.</p> + +<pre class="brush: html"><!doctype html> +<html> +<head> +<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> +<title>MDN Example - a recursive shortcut to massively modify the DOM</title> +<script type="text/javascript"> +var domQuery = (function() { + var aDOMFunc = [ + Element.prototype.removeAttribute, + Element.prototype.setAttribute, + CSSStyleDeclaration.prototype.removeProperty, + CSSStyleDeclaration.prototype.setProperty + ]; + + function setSomething(bStyle, sProp, sVal) { + var bSet = Boolean(sVal), fAction = aDOMFunc[bSet | bStyle << 1], + aArgs = Array.prototype.slice.call(arguments, 1, bSet ? 3 : 2), + aNodeList = bStyle ? this.cssNodes : this.nodes; + + if (bSet && bStyle) { aArgs.push(''); } + for ( + var nItem = 0, nLen = this.nodes.length; + nItem < nLen; + fAction.apply(aNodeList[nItem++], aArgs) + ); + this.follow = setSomething.caller; + return this; + } + + function setStyles(sProp, sVal) { return setSomething.call(this, true, sProp, sVal); } + function setAttribs(sProp, sVal) { return setSomething.call(this, false, sProp, sVal); } + function getSelectors() { return this.selectors; }; + function getNodes() { return this.nodes; }; + + return (function(sSelectors) { + var oQuery = new Function('return arguments.callee.follow.apply(arguments.callee, arguments);'); + oQuery.selectors = sSelectors; + oQuery.nodes = document.querySelectorAll(sSelectors); + oQuery.cssNodes = Array.prototype.map.call(oQuery.nodes, function(oInlineCSS) { return oInlineCSS.style; }); + oQuery.attributes = setAttribs; + oQuery.inlineStyle = setStyles; + oQuery.follow = getNodes; + oQuery.toString = getSelectors; + oQuery.valueOf = getNodes; + return oQuery; + }); +})(); +</script> +</head> + +<body> + +<div class="testClass">Lorem ipsum</div> +<p>Some text</p> +<div class="testClass">dolor sit amet</div> + +<script type="text/javascript"> +domQuery('.testClass') + .attributes('lang', 'en')('title', 'Risus abundat in ore stultorum') + .inlineStyle('background-color', 'black')('color', 'white')('width', '100px')('height', '50px'); +</script> +</body> + +</html> +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Implemented in JavaScript 1.0.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.3', 'Function')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-function-objects', 'Function')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("Functions", "Functions and function scope")}}</li> + <li>{{jsxref("Function")}}</li> + <li>{{jsxref("Statements/function", "function statement")}}</li> + <li>{{jsxref("Operators/function", "function expression")}}</li> + <li>{{jsxref("Statements/function*", "function* statement")}}</li> + <li>{{jsxref("Operators/function*", "function* expression")}}</li> + <li>{{jsxref("GeneratorFunction")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/function/length/index.html b/files/ca/web/javascript/reference/global_objects/function/length/index.html new file mode 100644 index 0000000000..2af00ebf41 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/function/length/index.html @@ -0,0 +1,134 @@ +--- +title: Function.length +slug: Web/JavaScript/Reference/Global_Objects/Function/length +translation_of: Web/JavaScript/Reference/Global_Objects/Function/length +--- +<div>{{JSRef}}</div> + +<p>La propietat <code><strong>length</strong></code> especifica el nombre d'arguments que la funció espera.</p> + +<div>{{js_property_attributes(0,0,1)}}</div> + +<h2 id="Descripció">Descripció</h2> + +<p><code>length</code> és una propietat d'un objecte funció, i indica quants arguments la funció espera, és a dir, el nombre de paràmetres formals. Aquest nombre no inclou el {{jsxref("rest_parameters", "rest parameter", "", 1)}}. Per contra, {{jsxref("Functions_and_function_scope/arguments/length", "arguments.length")}} és local a la funció i retorna el nombre d'arguments que s'han passat a la funció.</p> + +<h3 id="Propietat_Data_del_constructor_Function">Propietat Data del constructor <code>Function</code></h3> + +<p>El constructor {{jsxref("Function")}} és per ell mateix un objecte {{jsxref("Function")}}. La seva propietat data <code>length</code> pren un valor d'1. Els atributs de la propietat són: Writable: <code>false</code>, Enumerable: <code>false</code>, Configurable: <code>true</code>.</p> + +<h3 id="Propietat_de_l'objecte_prototipus_Function">Propietat de l'objecte prototipus <code>Function</code></h3> + +<p>La propietat length de l'objecte prototipus {{jsxref("Function")}} pren un valor de 0.</p> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">console.log(Function.length); /* 1 */ + +console.log((function() {}).length); /* 0 */ +console.log((function(a) {}).length); /* 1 */ +console.log((function(a, b) {}).length); /* 2 etc. */ +console.log((function(...args) {}).length); /* 0, la resta de paràmetres no es conta */ +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Definció inicial. Implementat en JavaScript 1.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.3.5.1', 'Function.length')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-function-instances-length', 'Function.length')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>L'atribut <code>configurable</code> d'aquesta propietat és ara <code>true</code>.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Configurable: true</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop(37)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Configurable: true</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(37)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Function")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/function/name/index.html b/files/ca/web/javascript/reference/global_objects/function/name/index.html new file mode 100644 index 0000000000..6f88ba9be2 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/function/name/index.html @@ -0,0 +1,153 @@ +--- +title: Function.name +slug: Web/JavaScript/Reference/Global_Objects/Function/name +translation_of: Web/JavaScript/Reference/Global_Objects/Function/name +--- +<div>{{JSRef}}</div> + +<p>La propietat <code><strong><em>function</em>.name</strong></code> retorna el nom de la funció.</p> + +<div>{{js_property_attributes(0,0,1)}}</div> + +<div>Vegeu que en les implementacions no estàndards anteriors a ES2015 l'atribut <code>configurable</code> també era <code>false</code>.</div> + +<h2 id="Descripció">Descripció</h2> + +<p>La propietat <code>name</code> retorna el nom de la funció, o una cadena buida per les funcions anònimes.</p> + +<pre class="brush: js">function fesAlgo() {} + +console.log(desAlgo.name); // escriu "fesAlgo" +</pre> + +<p>Les funcions creades amb la sintaxis <code>new Function(...)</code> o sols <code>Function(...)</code>tenen la seva propietat <code>name</code> en una cadena buida. En els exemples següents es creen funcions anònimes , de forma que <code>name</code> retorna una cadena buida:</p> + +<pre class="brush: js">var f = function() {}; +var object = { + someMethod: function() {} +}; + +console.log(f.name == ''); // true +console.log(object.someMethod.name == ''); // també true +</pre> + +<p>Es pot definir una funció amb un nom en un {{jsxref("Operators/Function", "function expression", "", 1)}}:</p> + +<pre class="brush: js">var object = { + someMethod: function object_someMethod() {} +}; +console.log(object.someMethod.name); // logs "object_someMethod" + +try { object_someMethod } catch(e) { console.log(e); } +// ReferenceError: object_someMethod no està definit +</pre> + +<p>No es pot canviar el nom de la funció, aquesta propietat és només llegible:</p> + +<pre class="brush: js">var object = { + // anònima + someMethod: function() {} +}; + +object.someMethod.name = 'someMethod'; +console.log(object.someMethod.name); // cadena buida, someMethod és anònima. +</pre> + +<p>Per canviar-ho, es pot utilitzar {{jsxref("Object.defineProperty()")}}.</p> + +<h2 id="Exemples">Exemples</h2> + +<p>Es pot utilitzar <code>obj.constructor.name</code> per comprovar la "classe" d'un objecte:</p> + +<pre class="brush: js">function a() {} + +var b = new a(); + +console.log(b.constructor.name); // escriu "a" +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-name', 'name')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Comptabilitat_amb_navegadors">Comptabilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>33</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Configurable: true</td> + <td>43</td> + <td>{{CompatGeckoDesktop(38)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Configurable: true</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(38)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> diff --git a/files/ca/web/javascript/reference/global_objects/function/tosource/index.html b/files/ca/web/javascript/reference/global_objects/function/tosource/index.html new file mode 100644 index 0000000000..27f135ce13 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/function/tosource/index.html @@ -0,0 +1,97 @@ +--- +title: Function.prototype.toSource() +slug: Web/JavaScript/Reference/Global_Objects/Function/toSource +translation_of: Web/JavaScript/Reference/Global_Objects/Function/toSource +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>El mètode <code><strong>toSource()</strong></code>retorna una cadena que representa el codi font de l'objecte.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><var>function</var>.toSource(); +Function.toSource(); +</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<p>Cap.</p> + +<h2 id="Descripció">Descripció</h2> + +<p>El mètode <code>toSource</code> retorna els valors següents:</p> + +<ul> + <li>Per l'objecte incorporat {{jsxref("Function")}}, <code>toSource()</code> retorna la cadena següent indicant que el codi font no es troba disponible: + + <pre class="brush: js">function Function() { + [native code] +} +</pre> + </li> + <li>Per funcions personalitzades, <code>toSource()</code> retorna la font de JavaScript que defineix l'objecte com una cadena.</li> +</ul> + +<p>Aquest mètode se sol cridar internament per JavaScript i no explícitament en el codi. Podeu cridar <code>toSource</code> durant la depuració per examinar el contingut d'un objecte.</p> + +<h2 id="Especificacions">Especificacions</h2> + +<p>No forma part de cap estàndard. Implementat en JavaScript 1.3.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Object.prototype.toSource()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/assign/index.html b/files/ca/web/javascript/reference/global_objects/object/assign/index.html new file mode 100644 index 0000000000..855b5654a6 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/assign/index.html @@ -0,0 +1,252 @@ +--- +title: Object.assign() +slug: Web/JavaScript/Reference/Global_Objects/Object/assign +translation_of: Web/JavaScript/Reference/Global_Objects/Object/assign +--- +<div>{{JSRef}}</div> + +<p>El mètode <strong><code>Object.assign()</code></strong> s'utilitza per copiar els valors de totes les propietats enumerables pròpies d'un o més objectes d'orígens a un objecte objectiu o destí. Retornarà l'objecte destí.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>Object.assign(<var>target</var>, ...<var>orígens</var>)</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>target</code></dt> + <dd>L'objecte destí.</dd> + <dt><code>sources</code></dt> + <dd>El(s) objecte(s) d'orígen.</dd> +</dl> + +<h3 id="Valor_retornat">Valor retornat</h3> + +<p>L'objecte destí (o objectiu) es retornat.</p> + +<h2 id="Descripció">Descripció</h2> + +<p>El mètode <code>Object.assign()</code> només copia propietats <em>enumerable</em>s i <em>pròpies</em> d'un objecte orígen a un objecte destí. S'utilitza <code>[[Get]]</code> a l'orígen i <code>[[Put]]</code> al destí, de forma que invocarà getters i setters. Per tant, <em>assigna</em> propietats en comptes de només copiar o definir noves propietats. Axò pot fer que sigui inadequat per juntar noves propietats en un prototipus si les fonts de la unió contenen getters. Per copiar definicions de la propietat, incloent la seva enumerabilitat, en prototipus s'ha de fer servir {{jsxref("Object.getOwnPropertyDescriptor()")}} i {{jsxref("Object.defineProperty()")}}.</p> + +<p>Tant la propietat {{jsxref("String")}} com la propietat {{jsxref("Symbol")}} són copiades.</p> + +<p>En cas d'error, per exemple si una propietat no és modificable, un {{jsxref("TypeError")}} <strong><u>s'incrementarà </u></strong>i l'objecte <code>target</code> object es mantindrà sense canvis.</p> + +<p>Vegeu que <code>Object.assign()</code> does not throw on {{jsxref("null")}} or {{jsxref("undefined")}} source values.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Clonar_un_objecte">Clonar un objecte</h3> + +<pre class="brush: js">var obj = { a: 1 }; +var copy = Object.assign({}, obj); +console.log(copy); // { a: 1 } +</pre> + +<h3 id="Unir_objectes">Unir objectes</h3> + +<pre class="brush: js">var o1 = { a: 1 }; +var o2 = { b: 2 }; +var o3 = { c: 3 }; + +var obj = Object.assign(o1, o2, o3); +console.log(obj); // { a: 1, b: 2, c: 3 } +console.log(o1); // { a: 1, b: 2, c: 3 }, L'objecte de destinació es canvia</pre> + +<h3 id="Copiar_propietats_symbol-typed">Copiar propietats symbol-typed</h3> + +<pre class="brush: js">var o1 = { a: 1 }; +var o2 = { [Symbol('foo')]: 2 }; + +var obj = Object.assign({}, o1, o2); +console.log(obj); // { a: 1, [Symbol("foo")]: 2 } +</pre> + +<h3 id="Les_propietats_heretades_i_les_propietats_no_enumerables_no_es_poden_copiar">Les propietats heretades i les propietats no enumerables no es poden copiar</h3> + +<pre class="brush: js">var obj = Object.create({ foo: 1 }, { // foo és una propietat heretada. + bar: { + value: 2 // bar és una propietat no enumerable. + }, + baz: { + value: 3, + enumerable: true // baz is an own enumerable property. + } +}); + +var copy = Object.assign({}, obj); +console.log(copy); // { baz: 3 } +</pre> + +<h3 id="Primitives_will_be_wrapped_to_objects">Primitives will be wrapped to objects</h3> + +<pre class="brush: js">var v1 = '123'; +var v2 = true; +var v3 = 10; +var v4 = Symbol('foo') + +var obj = Object.assign({}, v1, null, v2, undefined, v3, v4); +// Primitives will be wrapped, null and undefined will be ignored. +// Note, only string wrappers can have own enumerable properties. +console.log(obj); // { "0": "1", "1": "2", "2": "3" } +</pre> + +<h3 id="Exceptions_will_interrupt_the_ongoing_copying_task">Exceptions will interrupt the ongoing copying task</h3> + +<pre class="brush: js">var target = Object.defineProperty({}, 'foo', { + value: 1, + writeable: false +}); // target.foo is a read-only property + +Object.assign(target, { bar: 2 }, { foo2: 3, foo: 3, foo3: 3 }, { baz: 4 }); +// TypeError: "foo" is read-only +// The Exception is thrown when assigning target.foo + +console.log(target.bar); // 2, the first source was copied successfully. +console.log(target.foo2); // 3, the first property of the second source was copied successfully. +console.log(target.foo); // 1, exception is thrown here. +console.log(target.foo3); // undefined, assign method has finished, foo3 will not be copied. +console.log(target.baz); // undefined, the third source will not be copied either. +</pre> + +<h3 id="Copying_accessors">Copying accessors</h3> + +<pre class="brush: js">var obj = { + foo: 1, + get bar() { + return 2; + } +}; + +var copy = Object.assign({}, obj); +console.log(copy); +// { foo: 1, bar: 2 }, the value of copy.bar is obj.bar's getter's return value. + +// This is an assign function which can copy accessors. +function myAssign(target, ...sources) { + sources.forEach(source => { + Object.defineProperties(target, Object.keys(source).reduce((descriptors, key) => { + descriptors[key] = Object.getOwnPropertyDescriptor(source, key); + return descriptors; + }, {})); + }); + return target; +} + +var copy = myAssign({}, obj); +console.log(copy); +// { foo:1, get bar() { return 2 } } +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>This polyfill doesn't support symbol properties, since ES5 doesn't have symbols anyway:</p> + +<pre class="brush: js">if (!Object.assign) { + Object.defineProperty(Object, 'assign', { + enumerable: false, + configurable: true, + writable: true, + value: function(target) { + 'use strict'; + if (target === undefined || target === null) { + throw new TypeError('Cannot convert first argument to object'); + } + + var to = Object(target); + for (var i = 1; i < arguments.length; i++) { + var nextSource = arguments[i]; + if (nextSource === undefined || nextSource === null) { + continue; + } + nextSource = Object(nextSource); + + var keysArray = Object.keys(Object(nextSource)); + for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { + var nextKey = keysArray[nextIndex]; + var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); + if (desc !== undefined && desc.enumerable) { + to[nextKey] = nextSource[nextKey]; + } + } + } + return to; + } + }); +} +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-object.assign', 'Object.assign')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Definició inicial</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_de_navegadors">Compatibilitat de navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Caracteristica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome("45")}}</td> + <td>{{CompatGeckoDesktop("34")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("34")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Object.defineProperties()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/count/index.html b/files/ca/web/javascript/reference/global_objects/object/count/index.html new file mode 100644 index 0000000000..ed84c7006c --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/count/index.html @@ -0,0 +1,82 @@ +--- +title: Object.prototype.__count__ +slug: Web/JavaScript/Reference/Global_Objects/Object/count +translation_of: Archive/Web/JavaScript/Object.count +--- +<div>{{JSRef}} {{obsolete_header("2")}}</div> + +<p>La propietat <strong><code>__count__</code></strong> s'utilitzava per emmagatzemar el recompte de les propietats enumerables d'un objecte, però s'ha eliminat.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><var>obj</var>.__count__</code></pre> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">{ 1: 1 }.__count__ // 1 +[].__count__ // 0 +[1].__count__ // 1 +[1, /* hole */, 2, 3].__count__ // 3 +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<p>No forma part de cap especificació.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li><a class="external" href="http://whereswalden.com/2010/04/06/more-changes-coming-to-spidermonkey-the-magical-__count__-property-of-objects-is-being-removed/">[Blog post] Més canvis en SpiderMonkey: la propietat màgica __count__ s'ha eliminat</a></li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/eval/index.html b/files/ca/web/javascript/reference/global_objects/object/eval/index.html new file mode 100644 index 0000000000..8c43cf423a --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/eval/index.html @@ -0,0 +1,85 @@ +--- +title: Object.prototype.eval() +slug: Web/JavaScript/Reference/Global_Objects/Object/eval +translation_of: Archive/Web/JavaScript/Object.eval +--- +<div>{{JSRef}} {{obsolete_header}}</div> + +<p>El mètode <code><strong>Object.eval()</strong></code> solia avaluar una cadena del codi JavaScript en el context d'un objecte, tanmateix, aquest mètode s'ha eliminat.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><var>obj</var>.eval(<var>cadena</var>)</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>cadena</code></dt> + <dd>Qualsevol cadena que representi una expressió JavaScript, sentència, o una seqüència de sentències. L'expressió pot incloure variables i propietats d'objectes ja existents.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>El mètode <code>eval</code> ja no es pot utilitzar com a mètode d'un objecte. Utilitzeu la funció de més alt nivell {{jsxref("Global_Objects/eval", "eval()")}} per aquesta finalitat.</p> + +<h2 id="Especificacions">Especificacions</h2> + +<p>No forma part de cap especificació.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Global_Objects/eval", "eval()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/freeze/index.html b/files/ca/web/javascript/reference/global_objects/object/freeze/index.html new file mode 100644 index 0000000000..1231afe27f --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/freeze/index.html @@ -0,0 +1,193 @@ +--- +title: Object.freeze() +slug: Web/JavaScript/Reference/Global_Objects/Object/freeze +translation_of: Web/JavaScript/Reference/Global_Objects/Object/freeze +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>Object.freeze()</strong></code> congela un objecte: és a dir, evita que se li puguin afegir noves propietats, eliminar propietats ja existents així com modificar els paràmetres d'enumerabilitat, configurabilitat i possibilitat d'escriptura de les propietats existents. Això, en essència fa que l'objecte esdevingui immutable a efectes pràctics. El mètode retorna l'objecte que s'ha congelat.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>Object.freeze(<var>obj</var>)</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>L'objecte a congelar.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>Res pot ser afegit o eliminat del conjunt de propietats d'un objecte congelat. Qualsevol intent fallarà o bé sense reportar cap error o bé llençant una excepció {{jsxref("TypeError")}} (és l'excepció més freqüent que serà llençada però en pot llençat altres tipus; a l'utilitzar el {{jsxref("Strict_mode", "mode estricte", "", 1)}}).</p> + +<p>Congelar un objecte també evita que es puguin modificar els valors de les seves propietats. Les propietats d'accés (getters i setters) segueixen funcionan (donant la il·lusió que s'ha canviat el valor). Cal advertir, però, que els valors que siguin objectes sí que es poden modificar, a no ser que aquests objectes també estiguin congelats.</p> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">var obj = { + prop: function() {}, + foo: 'bar' +}; + +// Es poden afegir noves propietats, així com canviar o eliminar les propietats ja existents +obj.foo = 'baz'; +obj.lumpy = 'woof'; +delete obj.prop; + +var o = Object.freeze(obj); + +assert(Object.isFrozen(obj) === true); + +// A partir d'ara qualsevol canvi fallarà +obj.foo = 'quux'; // falla silenciosament +obj.quaxxor = 'the friendly duck'; // romàn en silenci i no afegeix la propietat + +// ...i en mode estricte qualsevol intent llençarà una excepció TypeError +function fail(){ + 'use strict'; + obj.foo = 'sparky'; // llença TypeError + delete obj.quaxxor; // llença TypeError + obj.sparky = 'arf'; // llença TypeError +} + +fail(); + +// Intentar realitzar canvis a través de Object.defineProperty també resultaran en excepcions +Object.defineProperty(obj, 'ohai', { value: 17 }); // llença TypeError +Object.defineProperty(obj, 'foo', { value: 'eit' }); // llença TypeError +</pre> + +<p>L'exemple següent demostra que valors de tipus objecte pertanyents a propietats d'un objecte congelat sí que es poden modificar.(<code>freeze</code> no s'aplica de manera recursiva).</p> + +<pre class="brush: js">obj1 = { + internal: {} +}; + +Object.freeze(obj1); +obj1.internal.a = 'aValue'; + +obj1.internal.a // 'aValue' + +// Per a fer que obj sigui totalment immutable cal congelar tots els objectes referenciats per aquest. +// Per a aconseguir això utilitzem la funció següent +function deepFreeze(obj) { + + // Obté els nomes de les propietats definides a l'objecte obj + var propNames = Object.getOwnPropertyNames(obj); + + // Congela les propietats abans de congelar l'objecte en si + propNames.forEach(function(name) { + var prop = obj[name]; + + // Congela prop si aquest és un objecte + if (typeof prop == 'object' && !Object.isFrozen(prop)) + deepFreeze(prop); + }); + + // Congela l'objecte pare + return Object.freeze(obj); +} + +obj2 = { + internal: {} +}; + +deepFreeze(obj2); +obj2.internal.a = 'anotherValue'; +obj2.internal.a; // undefined +</pre> + +<h2 id="Notes">Notes</h2> + +<p>A l'EcmaScript 5, si l'argument passat a aquest mètode no és un objecte (un valor primitiu), llençarà un {{jsxref("TypeError")}}. A l'EcmaScript 6, un argument que no sigui un objecte serà tractat com si fós un objecte congelat ordinari, i simplement el retornarà.</p> + +<pre class="brush: js">> Object.freeze(1) +TypeError: 1 no és un objecte // Codi EcmaScript 5 + +> Object.freeze(1) +1 // Codi EcmaScript 6 +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificacions</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.3.9', 'Object.freeze')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definició inicial. Implementat a JavaScript 1.8.5.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.freeze', 'Object.freeze')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Firefox (Gecko)</th> + <th>Chrome</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatGeckoDesktop("2")}}</td> + <td>{{CompatChrome("6")}}</td> + <td>{{CompatIE("9")}}</td> + <td>{{CompatOpera("12")}}</td> + <td>{{CompatSafari("5.1")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Firefox Mobile (Gecko)</th> + <th>Android</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Object.isFrozen()")}}</li> + <li>{{jsxref("Object.preventExtensions()")}}</li> + <li>{{jsxref("Object.isExtensible()")}}</li> + <li>{{jsxref("Object.seal()")}}</li> + <li>{{jsxref("Object.isSealed()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/getprototypeof/index.html b/files/ca/web/javascript/reference/global_objects/object/getprototypeof/index.html new file mode 100644 index 0000000000..01666d0898 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/getprototypeof/index.html @@ -0,0 +1,124 @@ +--- +title: Object.getPrototypeOf() +slug: Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf +translation_of: Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>Object.getPrototypeOf()</strong></code> retorna el prototipus (és a dir, el valor de la propietat interna <code>[[Prototype]]</code>) de l'objecte especificat.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>Object.getPrototypeOf(<var>obj</var>)</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>L'objecte del qual es retornarà el prototipus.</dd> +</dl> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">var proto = {}; +var obj = Object.create(proto); +Object.getPrototypeOf(obj) === proto; // true +</pre> + +<h2 id="Notes">Notes</h2> + +<p>A l'EcmaScript 5 llençarà una excepció {{jsxref("TypeError")}} si el paràmetre <code>obj</code> no és un objecte. A l'EcmaScript 6 el paràmetre serà forçat al tipus {{jsxref("Object")}}.</p> + +<pre class="brush: js">Object.getPrototypeOf("foo"); +// TypeError: "foo" no és un objecte (codi EcmaScript 5) +Object.getPrototypeOf("foo"); +// String.prototype (codi EcmaScript 6) +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.3.2', 'Object.getPrototypeOf')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definició inicial.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.getprototypeof', 'Object.getProtoypeOf')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome("5")}}</td> + <td>{{CompatGeckoDesktop("1.9.1")}}</td> + <td>{{CompatIE("9")}}</td> + <td>{{CompatOpera("12.10")}}</td> + <td>{{CompatSafari("5")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Notes_específiques_per_a_Opera">Notes específiques per a Opera</h2> + +<p>Tot i que versions antigues de Opera no suporten <code>Object.getPrototypeOf()</code>, Opera soporta la propietat no standard {{jsxref("Object.proto", "__proto__")}} a partir de la versió 10.50.</p> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Object.prototype.isPrototypeOf()")}}</li> + <li>{{jsxref("Object.setPrototypeOf()")}} {{experimental_inline}}</li> + <li>{{jsxref("Object.prototype.__proto__")}}</li> + <li>John Resig's post on <a class="external" href="http://ejohn.org/blog/objectgetprototypeof/">getPrototypeOf</a></li> + <li>{{jsxref("Reflect.getPrototypeOf()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/index.html b/files/ca/web/javascript/reference/global_objects/object/index.html new file mode 100644 index 0000000000..1c2a779065 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/index.html @@ -0,0 +1,213 @@ +--- +title: Object +slug: Web/JavaScript/Reference/Global_Objects/Object +tags: + - Constructor + - JavaScript + - NeedsTranslation + - Object + - TopicStub +translation_of: Web/JavaScript/Reference/Global_Objects/Object +--- +<div>{{JSRef}}</div> + +<p>El constructor <code><strong>Object</strong></code> crea un embolcall d'objecte.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>// Inicialitzador d'objecte o literal +{ [ <var>parellNomValor1</var>[, <var>parellNomValor2</var>[, ...<var>parellNomValorN</var>] ] ] } + +// Cridat com a constructor +new Object([valor])</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>parellNomValor1, parellNomValor2, ... parellNomValor<em>N</em></code></dt> + <dd>Parells de noms (string) i valors (quasevol valor) on els noms se separen dels valors mitjançant dos punts (:).</dd> + <dt><code>value</code></dt> + <dd>Qualsevol valor.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>El constructor <code>Object</code> crea un embolcall d'objecte per al valor donat. Si el valor és {{jsxref("null")}} o bé {{jsxref("undefined")}}, crearà i retornarà un objecte buit. En cas contrari retornarà un objecte del tipus corresponent al valor donat. Si el valor donat ja és un objecte, retornarà el mateix objecte.</p> + +<p>Quan es crida fora d'un contexte constructor, <code>Object</code> es comporta exactament de la mateixa manera que <code>new Object()</code>.</p> + +<p>Vegeu també la <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">sintaxi literal / d'inicialització d'objectes</a>.</p> + +<h2 id="Propietats_del_constructor_Object">Propietats del constructor <code>Object</code></h2> + +<dl> + <dt><code>Object.length</code></dt> + <dd>Val 1.</dd> + <dt>{{jsxref("Object.prototype")}}</dt> + <dd>Permet l'adició de propietats a tots els objecte del tipus <code>Object</code>.</dd> +</dl> + +<h2 id="Mètodes_del_constructor_Object">Mètodes del constructor <code>Object</code></h2> + +<dl> + <dt>{{jsxref("Object.assign()")}} {{experimental_inline}}</dt> + <dd>Crea un nou objecte copiant els valors de totes les propietats pròpies enumerables d'un o més objectes origin a l'objecte destí.</dd> + <dt>{{jsxref("Object.create()")}}</dt> + <dd>Crea un nou objecte amb l'objecte prototipus que s'especifiqui, així com les propietats passades.</dd> + <dt>{{jsxref("Object.defineProperty()")}}</dt> + <dd>Afegeix a l'objecte la propietat amb nom descrita pel descriptor especificat.</dd> + <dt>{{jsxref("Object.defineProperties()")}}</dt> + <dd>Afegeix a l'objecte les propietats amb nom descrites pels descriptors especificats.</dd> + <dt>{{jsxref("Object.freeze()")}}</dt> + <dd><em>Congela</em> un objecte de forma que les propietats d'aquest no poden ser esborrades o canviades.</dd> + <dt>{{jsxref("Object.getOwnPropertyDescriptor()")}}</dt> + <dd>Retorna un descriptor de propietat per a la propietat donada d'un objecte.</dd> + <dt>{{jsxref("Object.getOwnPropertyNames()")}}</dt> + <dd>Retorna un array que contindrà els nomes de totes les propietats <strong>pròpies</strong> d'un objecte, tant enumerables com no enumerables.</dd> + <dt>{{jsxref("Object.getOwnPropertySymbols()")}} {{experimental_inline}}</dt> + <dd>Retorna un array amb totes les propietats de tipus symbol que siguin pròpies d'un objecte donat.</dd> + <dt>{{jsxref("Object.getPrototypeOf()")}}</dt> + <dd>Retorna el prototipus de l'objecte especificat.</dd> + <dt>{{jsxref("Object.is()")}} {{experimental_inline}}</dt> + <dd>Compara dos valors i determina si són equivalents (és a dir, si són el mateix objecte).</dd> + <dt>{{jsxref("Object.isExtensible()")}}</dt> + <dd>Determina si és permés extendre un objecte.</dd> + <dt>{{jsxref("Object.isFrozen()")}}</dt> + <dd>Determina si l'objecte està congelat.</dd> + <dt>{{jsxref("Object.isSealed()")}}</dt> + <dd>Determina si un objecte està segellat.</dd> + <dt>{{jsxref("Object.keys()")}}</dt> + <dd>Retorna un array que conté els noms de totes les propietats enumerables <strong>pròpies</strong> d'un objecte donat.</dd> + <dt>{{jsxref("Object.observe()")}} {{experimental_inline}}</dt> + <dd>Observa canvies en un objecte de forma asíncrona.</dd> + <dt>{{jsxref("Object.preventExtensions()")}}</dt> + <dd>Desactiva la capacitat d'un objecte per a ser extés.</dd> + <dt>{{jsxref("Object.seal()")}}</dt> + <dd>Desactiva la capacitat de poder esborrar propietats de l'objecte.</dd> + <dt>{{jsxref("Object.setPrototypeOf()")}} {{experimental_inline}}</dt> + <dd>Assigna el prototipus (és a dir, la propietat interna <code>[[Prototype]]</code>)</dd> +</dl> + +<h2 id="Instàncies_de_Object_i_l'objecte_prototipus">Instàncies de <code>Object</code> i l'objecte prototipus</h2> + +<p>A JavaScript, tots els objectes són descendents de <code>Object</code>; tots els objectes hereten els mètodes i propietats de {{jsxref("Object.prototype")}}, tot i que poden ser sobreescrits. Per exemple, els prototipus d'altres constructors sobreescriuen la propietat <code>constructor</code> i ofereixen el seu propi mètode <code>toString()</code>. Els canvis al prototipus <code>Object</code> es propaguen a tots els objectes a no ser que les propietats i mètodes que reben aquests canvis hagin sigut sobreescrites més avall a la cadena de prototipus.</p> + +<h3 id="Propietats">Propietats</h3> + +<div>{{page('/ca/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype', 'Properties') }}</div> + +<h3 id="Mètodes">Mètodes</h3> + +<div>{{page('/ca/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype', 'Methods') }}</div> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_Object_amb_els_tipus_undefined_i_null_types">Utilitzar <code>Object</code> amb els tipus <code>undefined</code> i <code>null</code> types</h3> + +<p>Els següents exemples emmagatzemen un objecte <code>Object</code> buit a <code>o</code>:</p> + +<pre class="brush: js">var o = new Object(); +</pre> + +<pre class="brush: js">var o = new Object(undefined); +</pre> + +<pre class="brush: js">var o = new Object(null); +</pre> + +<h3 id="Utilitzar_Object_per_a_crear_objectes_booleans">Utilitzar <code>Object</code> per a crear objectes booleans</h3> + +<p>Els exemples següents emmagatzemen objectes de tipus {{jsxref("Boolean")}} a <code>o</code>:</p> + +<pre class="brush: js">// equivalent a o = new Boolean(true); +var o = new Object(true); +</pre> + +<pre class="brush: js">// equivalent a o = new Boolean(false); +var o = new Object(Boolean()); +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Definició inicial. Implementat a JavaScript 1.0.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2', 'Object')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object-objects', 'Object')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer">Inicialitzador d'objectes</a></li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/isextensible/index.html b/files/ca/web/javascript/reference/global_objects/object/isextensible/index.html new file mode 100644 index 0000000000..7b8700654c --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/isextensible/index.html @@ -0,0 +1,138 @@ +--- +title: Object.isExtensible() +slug: Web/JavaScript/Reference/Global_Objects/Object/isExtensible +translation_of: Web/JavaScript/Reference/Global_Objects/Object/isExtensible +--- +<div>{{JSRef}}</div> + +<p>El mètode <strong><code>Object.isExtensible()</code></strong> determina si un objecte és extensible (és a dir, si accepta l'addició de noves propietats).</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>Object.isExtensible(<var>obj</var>)</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>L'objecte del que comprovarà l'extensibilitat.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>Per defecte tots els objectes són extensibles: se'ls hi poden afegir noves propietats, i (en objectes que suportin {{jsxref("Object.proto", "__proto__")}} {{deprecated_inline}} la seva propietat __proto__ property) es pot modificar. Es pot marcar un objecte com a no extensible utilitzant {{jsxref("Object.preventExtensions()")}}, {{jsxref("Object.seal()")}}, o bé {{jsxref("Object.freeze()")}}.</p> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">// Els objectes nous són extensibles. +var empty = {}; +Object.isExtensible(empty); // === true + +// ...pero això pot canviar. +Object.preventExtensions(empty); +Object.isExtensible(empty); // === false + +// Els objectes segellats són no extensibles per definició +var sealed = Object.seal({}); +Object.isExtensible(sealed); // === false + +// Els objectes congelats també són no extensibes per definició +var frozen = Object.freeze({}); +Object.isExtensible(frozen); // === false +</pre> + +<h2 id="Notes">Notes</h2> + +<p>A l'EcmaScript 5, si l'argument d'aquest mètode no és un objecte (un valor primitiu) es llençarà una excepció {{jsxref("TypeError")}}. A l'EcmaScript 6, un argument que no sigui un objecte es tractarà com si fós un objecte no extensible ordinari i simplement retornarà <code>false</code>.</p> + +<pre class="brush: js">Object.isExtensible(1); +// TypeError: 1 no és un objecte (codi EcmaScript 5) + +Object.isExtensible(1); +// false (codi EcmaScript 6) +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.3.13', 'Object.isExtensible')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definició inicial. Implementat a JavaScript 1.8.5.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.isextensible', 'Object.isExtensible')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome("6")}}</td> + <td>{{CompatGeckoDesktop("2.0")}}</td> + <td>{{CompatIE("9")}}</td> + <td>{{CompatOpera("12")}}</td> + <td>{{CompatSafari("5.1")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Object.preventExtensions()")}}</li> + <li>{{jsxref("Object.seal()")}}</li> + <li>{{jsxref("Object.isSealed()")}}</li> + <li>{{jsxref("Object.freeze()")}}</li> + <li>{{jsxref("Object.isFrozen()")}}</li> + <li>{{jsxref("Reflect.isExtensible()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/isfrozen/index.html b/files/ca/web/javascript/reference/global_objects/object/isfrozen/index.html new file mode 100644 index 0000000000..46c2e24be2 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/isfrozen/index.html @@ -0,0 +1,182 @@ +--- +title: Object.isFrozen() +slug: Web/JavaScript/Reference/Global_Objects/Object/isFrozen +translation_of: Web/JavaScript/Reference/Global_Objects/Object/isFrozen +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>Object.isFrozen()</strong></code> determina si un objecte està congelat.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>Object.isFrozen(<var>obj</var>)</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>L'objecte que es comprovarà si està congelat o no.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>Un objecte està congelat si i només si no és {{jsxref("Object.isExtensible()", "extensible", "", 1)}}, cap de les seves propietats és configurable, i cap de les seves propietats de dades (és a dir, propietats que no són <em>accessor</em> amb components getter o setter) that is, properties which are not accessor properties with getter or setter components) es poden escriure (modificar el seu valor).</p> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">// Els objectes nous són extensibles, així que no estan congelats. +Object.isFrozen({}); // === false + +// Un objecte buit que no és extensible està congelat ja que no te propietats que trenquin les restriccions. +var vacuouslyFrozen = Object.preventExtensions({}); +Object.isFrozen(vacuouslyFrozen); // === true + +// Un objecte nou amb una propietat és extensible, i per tant no està congelat. +var oneProp = { p: 42 }; +Object.isFrozen(oneProp); // === false + +// Fer-lo no extensible no el fa congelat, +// perquè la propietat encara és configurable (i permet l'escriptura). +Object.preventExtensions(oneProp); +Object.isFrozen(oneProp); // === false + +// ...però, un altre cop, si eliminem la propietat ens trobem amb un objecte buit congelat. +delete oneProp.p; +Object.isFrozen(oneProp); // === true + +// Un objecte no extensible amb una propietat que no permeti l'escriptura però sí que es configurable no està congelat. +var nonWritable = { e: 'plep' }; +Object.preventExtensions(nonWritable); +Object.defineProperty(nonWritable, 'e', { writable: false }); // fer que la propietat no permeti l'escriptura +Object.isFrozen(nonWritable); // === false + +// Fer aquesta propietat no configurable fa que l'objecte estigui congelat +Object.defineProperty(nonWritable, 'e', { configurable: false }); // fer la propietat no configurable +Object.isFrozen(nonWritable); // === true + +// Un objecte no extensible amb una propietat no configurable però que si permeti l'escriptura tampoc està congelat. +var nonConfigurable = { release: 'the kraken!' }; +Object.preventExtensions(nonConfigurable); +Object.defineProperty(nonConfigurable, 'release', { configurable: false }); +Object.isFrozen(nonConfigurable); // === false + +// Canviar aquesta propietat per a que no permeti l'escriptura fa que l'objecte estigui congelat. +Object.defineProperty(nonConfigurable, 'release', { writable: false }); +Object.isFrozen(nonConfigurable); // === true + +// Un objecte amb una propietat accessor no extensible no està congelat. +var accessor = { get food() { return 'yum'; } }; +Object.preventExtensions(accessor); +Object.isFrozen(accessor); // === false + +// ...però si la propietat es fa no configurable l'objecte esdevé congelat. +Object.defineProperty(accessor, 'food', { configurable: false }); +Object.isFrozen(accessor); // === true + +// La forma més fàcil, però, d'aconseguir congelar un objecte és cridant el mètode Object.freeze al mateix objecte. +var frozen = { 1: 81 }; +Object.isFrozen(frozen); // === false +Object.freeze(frozen); +Object.isFrozen(frozen); // === true + +// Per definició, un objecte congelat no és extensible. +Object.isExtensible(frozen); // === false + +// També per definició, un objecte congelat està segellat. +Object.isSealed(frozen); // === true +</pre> + +<h2 id="Notes">Notes</h2> + +<p>A l'EcmaScript 5, si l'argument passat a aquest mètode no és un objecte (un valor primitiu), llençarà un <a href="https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Objectes_globals/TypeError" title="L'objecte TypeError representa un error quan el valor no és del tipus esperat."><code>TypeError</code></a>. A l'EcmaScript 6, un argument que no sigui un objecte serà tractat com si fós un objecte congelat ordinari, i simplement el retornarà.</p> + +<pre class="brush: js">Object.isFrozen(1); +// TypeError: 1 no és un objecte (codi EcmaScript 5) + +Object.isFrozen(1); +// true (codi EcmaScript 6) +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.3.12', 'Object.isFrozen')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definició inicial. Implementat a JavaScript 1.8.5.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.isfrozen', 'Object.isFrozen')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome("6")}}</td> + <td>{{CompatGeckoDesktop("2.0")}}</td> + <td>{{CompatIE("9")}}</td> + <td>{{CompatOpera("12")}}</td> + <td>{{CompatSafari("5.1")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Object.freeze()")}}</li> + <li>{{jsxref("Object.preventExtensions()")}}</li> + <li>{{jsxref("Object.isExtensible()")}}</li> + <li>{{jsxref("Object.seal()")}}</li> + <li>{{jsxref("Object.isSealed()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/keys/index.html b/files/ca/web/javascript/reference/global_objects/object/keys/index.html new file mode 100644 index 0000000000..17d05b181c --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/keys/index.html @@ -0,0 +1,189 @@ +--- +title: Object.keys() +slug: Web/JavaScript/Reference/Global_Objects/Object/keys +translation_of: Web/JavaScript/Reference/Global_Objects/Object/keys +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>Object.keys()</strong></code> retorna un array de les propietats enumerables pròpies de l'objecte, en el mateix ordre en que es donarien en un bucle {{jsxref("Statements/for...in", "for...in")}} (la diferència radica en que el bucle for-in també enumera les propietats que pertanyen a la cadena de prototipus).</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>Object.keys(<var>obj</var>)</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>L'objecte del qual es retornaran les seves propietats pròpies enumerables.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p><code>Object.keys()</code> retorna un array els elements del qual són strings que corresponen a les propietats enumerables que pertanyen directament a l'objecte <code>obj</code>. L'ordre de les propietats és el mateix que el donat per recòrrer les propieats de l'objecte de forma manual.</p> + +<h2 id="Exemples">Exemples</h2> + +<pre class="brush: js">var arr = ['a', 'b', 'c']; +console.log(Object.keys(arr)); // console: ['0', '1', '2'] + +// objecte en forma d'array +var obj = { 0: 'a', 1: 'b', 2: 'c' }; +console.log(Object.keys(obj)); // console: ['0', '1', '2'] + +// objecte en forma d'array amb les claus ordenades de manera aleatòria +var an_obj = { 100: 'a', 2: 'b', 7: 'c' }; +console.log(Object.keys(an_obj)); // console: ['2', '7', '100'] + +// getFoo és una propietat no enumerable +var my_obj = Object.create({}, { getFoo: { value: function() { return this.foo; } } }); +my_obj.foo = 1; + +console.log(Object.keys(my_obj)); // console: ['foo'] +</pre> + +<p>Si esteu interessats en obtenir totes les propietats, no només les enumerables, vegeu {{jsxref("Object.getOwnPropertyNames()")}}.</p> + +<h2 id="Notes">Notes</h2> + +<p>A l'EcmaScript 5 llençarà una excepció <a href="https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Objectes_globals/TypeError" title="L'objecte TypeError representa un error quan el valor no és del tipus esperat."><code>TypeError</code></a> si el paràmetre <code>obj</code> no és un objecte. A l'EcmaScript 6 el paràmetre serà forçat al tipus <a class="new" href="https://developer.mozilla.org/ca/docs/Web/JavaScript/Referencia/Object" title="Aquesta pàgina encara no ha estat traduïda. Si us plau considera contribuir-hi!"><code>Object</code></a>.</p> + +<pre class="brush: js">Object.keys("foo"); +// TypeError: "foo" no és un objecte (codi EcmaScript 5) +Object.keys("foo"); +// ["0", "1", "2"] (codi EcmaScript 6) +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>Per a afegit una funció compatible amb <code>Object.keys</code> en plataformes que no la suporten de forma nativa, podeu utilitzar el codi següent:</p> + +<pre class="brush: js">// Tret de https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys +if (!Object.keys) { + Object.keys = (function() { + 'use strict'; + var hasOwnProperty = Object.prototype.hasOwnProperty, + hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'), + dontEnums = [ + 'toString', + 'toLocaleString', + 'valueOf', + 'hasOwnProperty', + 'isPrototypeOf', + 'propertyIsEnumerable', + 'constructor' + ], + dontEnumsLength = dontEnums.length; + + return function(obj) { + if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) { + throw new TypeError('Object.keys called on non-object'); + } + + var result = [], prop, i; + + for (prop in obj) { + if (hasOwnProperty.call(obj, prop)) { + result.push(prop); + } + } + + if (hasDontEnumBug) { + for (i = 0; i < dontEnumsLength; i++) { + if (hasOwnProperty.call(obj, dontEnums[i])) { + result.push(dontEnums[i]); + } + } + } + return result; + }; + }()); +} +</pre> + +<p>Cal destacar que aquest codi inclou també propietats no enumerables a Internet Explorer 7 (i possiblement també a Internet Explorer 8), al passar un objecte pertanyent a una altra finestra.</p> + +<p>Per a una versió simplificada Polyfill per a navegadors vegeu <a href="http://tokenposts.blogspot.com.au/2012/04/javascript-objectkeys-browser.html">Compatibilitat de navegadors amb la funció Object.keys de JavaScript</a>.</p> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.3.14', 'Object.keys')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td>Definició inicial. Implementat a 1.8.5.</td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.keys', 'Object.keys')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome("5")}}</td> + <td>{{CompatGeckoDesktop("2.0")}}</td> + <td>{{CompatIE("9")}}</td> + <td>{{CompatOpera("12")}}</td> + <td>{{CompatSafari("5")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li><a href="/en-US/docs/Enumerability_and_ownership_of_properties">Enumerabilitat i possessió de propietats</a></li> + <li>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</li> + <li>{{jsxref("Object.create()")}}</li> + <li>{{jsxref("Object.getOwnPropertyNames()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/observe/index.html b/files/ca/web/javascript/reference/global_objects/object/observe/index.html new file mode 100644 index 0000000000..7059b86cd8 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/observe/index.html @@ -0,0 +1,191 @@ +--- +title: Object.observe() +slug: Web/JavaScript/Reference/Global_Objects/Object/observe +translation_of: Archive/Web/JavaScript/Object.observe +--- +<div>{{JSRef}}</div> + +<p>El mètode <strong><code>Object.observe()</code></strong> s'utilitza per observar asincrònicament els canvis en un objecte. Proveeix una corrent de canvis en l'ordre en què es produeixen.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code>Object.observe(<var>obj</var>, <var>callback</var>[, <var>acceptList</var>])</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>obj</code></dt> + <dd>L'objecte que s'observa.</dd> + <dt><code>callback</code></dt> + <dd>La funció es crida cada cop que es realitzen canvis, amb l'argument següent: + <dl> + <dt><code>changes</code></dt> + <dd>Un array d'objectes cadascún d'ells representa un canvi. Les propietats d'aquests objectes canvi són: + <ul> + <li><strong><code>name</code></strong>: El nom de la propietat que s'ha canviat.</li> + <li><strong><code>object</code></strong>: L'objecte canviat després d'haverse realitzat els canvis.</li> + <li><strong><code>type</code></strong>: Una cadena que indica el tipus de canvi que s'ha portat a terme: <code>"add"</code>, <code>"update"</code>, o <code>"delete"</code>.</li> + <li><strong><code>oldValue</code></strong>: Només pels tipus <code>"update"</code> i <code>"delete"</code>. El valor abans del canvi.</li> + </ul> + </dd> + </dl> + </dd> + <dt><code>acceptList</code></dt> + <dd>La llista de tipus de canvis que s'han d'observar en l'objecte donat callback. En cas d'ometre's, s'utilitzarà l'array <code>["add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions"]</code>.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p><code>callback</code> és cridat cada cop que es realitza un canvi a <code>obj</code>, amb un array de tots els canvis en l'ordre en que han succeït.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Mostrant_tots_els_sis_tipus_diferents">Mostrant tots els sis tipus diferents</h3> + +<pre class="brush: js">var obj = { + foo: 0, + bar: 1 +}; + +Object.observe(obj, function(changes) { + console.log(changes); +}); + +obj.baz = 2; +// [{name: 'baz', object: <obj>, type: 'add'}] + +obj.foo = 'hello'; +// [{name: 'foo', object: <obj>, type: 'update', oldValue: 0}] + +delete obj.baz; +// [{name: 'baz', object: <obj>, type: 'delete', oldValue: 2}] + +Object.defineProperty(obj, 'foo', {writable: false}); +// [{name: 'foo', object: <obj>, type: 'reconfigure'}] + +Object.setPrototypeOf(obj, {}); +// [{name: '__proto__', object: <obj>, type: 'setPrototype', oldValue: <prototype>}] + +Object.seal(obj); +// [ +// {name: 'foo', object: <obj>, type: 'reconfigure'}, +// {name: 'bar', object: <obj>, type: 'reconfigure'}, +// {object: <obj>, type: 'preventExtensions'} +// ] +</pre> + +<h3 id="Enllaçar_dades">Enllaçar dades</h3> + +<pre class="brush: js">// Un model d'usuari +var user = { + id: 0, + name: 'Brendan Eich', + title: 'Mr.' +}; + +// Crear una benvinguda per l'usuari +function updateGreeting() { + user.greeting = 'Hello, ' + user.title + ' ' + user.name + '!'; +} +updateGreeting(); + +Object.observe(user, function(changes) { + changes.forEach(function(change) { + // Qualsevol canvi de nom del temps o títol, actualitzar la benvinguda + if (change.name === 'name' || change.name === 'title') { + updateGreeting(); + } + }); +}); +</pre> + +<h3 id="Tipus_de_canvi_personalitzat">Tipus de canvi personalitzat</h3> + +<pre class="brush: js">// Un punt en un pla 2D +var point = {x: 0, y: 0, distance: 0}; + +function setPosition(pt, x, y) { + // Performing a custom change + Object.getNotifier(pt).performChange('reposition', function() { + var oldDistance = pt.distance; + pt.x = x; + pt.y = y; + pt.distance = Math.sqrt(x * x + y * y); + return {oldDistance: oldDistance}; + }); +} + +Object.observe(point, function(changes) { + console.log('Distance change: ' + (point.distance - changes[0].oldDistance)); +}, ['reposition']); + +setPosition(point, 3, 4); +// Distance change: 5 +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<p><a href="https://github.com/arv/ecmascript-object-observe">Proposta de Strawman per ECMAScript 7</a>.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome("36")}}</td> + <td>{{CompatNo}} [1]</td> + <td>{{CompatNo}} [2]</td> + <td>{{CompatOpera("23")}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome("36")}}</td> + <td>{{CompatNo}} [1]</td> + <td>{{CompatNo}} [2]</td> + <td>{{CompatOpera("23")}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<p>[1]: Vegeu {{bug(800355)}}</p> + +<p>[2]: Vegeu rellevant <a href="https://dev.modern.ie/platform/status/objectobserve/">entrada de l'estat de la plataforma MS Edge</a></p> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Object.unobserve()")}} {{experimental_inline}}</li> + <li>{{jsxref("Array.observe()")}} {{experimental_inline}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/object/prototype/index.html b/files/ca/web/javascript/reference/global_objects/object/prototype/index.html new file mode 100644 index 0000000000..b18ecc2d5a --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/object/prototype/index.html @@ -0,0 +1,214 @@ +--- +title: Object.prototype +slug: Web/JavaScript/Reference/Global_Objects/Object/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Object +--- +<div>{{JSRef}}</div> + +<p>La propietat <code><strong>Object.prototype</strong></code> representa el prototipus per a {{jsxref("Object")}}.</p> + +<div>{{js_property_attributes(0, 0, 0)}}</div> + +<h2 id="Descripció">Descripció</h2> + +<p>A JavaScript, tots els objectes hereten de {{jsxref("Object")}}; tots els objectes hereten els mètodes i propietats de <code>Object.prototype</code>, tot i que es poden sobreescriure (excepte un <code>Object</code> amb prototipus <code>null</code>, és a dir, <code>Object.create(null)</code>). Per exemple, altres prototipus de constructors sobreescriuen la propietat <code>constructor</code> i ofereixen els seus propis mètodes {{jsxref("Object.prototype.toString()", "toString()")}}. Els canvis al prototipus <code>Object</code> es propaguen a tots els objectes a no ser que les propietats i mètodes que reben aquests canvis hagin sigut sobreescrites més avall a la cadena de prototipus.</p> + +<h2 id="Propietats">Propietats</h2> + +<dl> + <dt>{{jsxref("Object.prototype.constructor")}}</dt> + <dd>Especifica la funció que ha creat el prototipus de l'objecte.</dd> + <dt>{{jsxref("Object.prototype.__proto__")}} {{non-standard_inline}}</dt> + <dd>Referencia l'objecte utilitzat com a prototipus quan aquest objecte va ser instanciat.</dd> + <dt>{{jsxref("Object.prototype.__noSuchMethod__")}} {{non-standard_inline}}</dt> + <dd>Permet definir una funció que serà executada quan es cridi com mètode un membre no definit.</dd> + <dt><s class="obsoleteElement">{{jsxref("Object.prototype.__count__")}} {{obsolete_inline}}</s></dt> + <dd><s class="obsoleteElement">Retornava el nombre de propietats enumerables que hi hagués a un objecte definit per l'usuari. S'ha eliminat.</s></dd> + <dt><s class="obsoleteElement">{{jsxref("Object.prototype.__parent__")}} {{obsolete_inline}}</s></dt> + <dd><s class="obsoleteElement">Referenciava el context d'un objecte. S'ha eliminat.</s></dd> +</dl> + +<h2 id="Mètodes">Mètodes</h2> + +<dl> + <dt>{{jsxref("Object.prototype.__defineGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt> + <dd>Associa una funció a una propietat que, quan s'accedeix, executa aquesta funció i retorna el seu valor.</dd> + <dt>{{jsxref("Object.prototype.__defineSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt> + <dd>Associa una funció a una propietat que, quan s'assigna, executa aquesta funció que modifica la propietat.</dd> + <dt>{{jsxref("Object.prototype.__lookupGetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt> + <dd>Retorna una funció associada a la propietat especificada pel mètode {{jsxref("Object.defineGetter", "__defineGetter__")}}.</dd> + <dt>{{jsxref("Object.prototype.__lookupSetter__()")}} {{non-standard_inline}} {{deprecated_inline}}</dt> + <dd>Retorna a funció associada a la propietat especificada pel mètode {{jsxref("Object.defineSetter", "__defineSetter__")}}.</dd> + <dt>{{jsxref("Object.prototype.hasOwnProperty()")}}</dt> + <dd>Retorna un booleà que indica si l'objecte conté la propietat especificada com una propietat pròpia d'ell mateix en comptes d'heretar-la a través de la cadena de prototipus.</dd> + <dt>{{jsxref("Object.prototype.isPrototypeOf()")}}</dt> + <dd>Retorna un booleà que indica si l'objecte espeicfifcat pertany a la cadena de prototipus de l'objecte sobre el que es crida aquest mètode.</dd> + <dt>{{jsxref("Object.prototype.propertyIsEnumerable()")}}</dt> + <dd>Retorna un booleà que indica si està activat l'atribut intern <a href="/en-US/docs/ECMAScript_DontEnum_attribute" title="ECMAScript_DontEnum_attribute">DontEnum de l'ECMAScript</a>.</dd> + <dt>{{jsxref("Object.prototype.toSource()")}} {{non-standard_inline}}</dt> + <dd>Retorna un string que conté cofi font que defineix un literal d'objecte que representa l'objecte sobre el que s'executa aquest mètode; aquest valor pot utilitzar-se per a crear un nou objecte.</dd> + <dt>{{jsxref("Object.prototype.toLocaleString()")}}</dt> + <dd>Crida el mètode {{jsxref("Object.toString", "toString()")}}.</dd> + <dt>{{jsxref("Object.prototype.toString()")}}</dt> + <dd>Retorna una representació d'aquest objecte en forma de string.</dd> + <dt>{{jsxref("Object.prototype.unwatch()")}} {{non-standard_inline}}</dt> + <dd>Esborra un <em>watchpoint</em> d'una propietat de l'objecte.</dd> + <dt>{{jsxref("Object.prototype.valueOf()")}}</dt> + <dd>Retorna el valor primitiu de l'objecte especificat.</dd> + <dt>{{jsxref("Object.prototype.watch()")}} {{non-standard_inline}}</dt> + <dd>Afegeix un <em>watchpoint</em> a una propietat de l'objecte.</dd> + <dt><s class="obsoleteElement">{{jsxref("Object.prototype.eval()")}} {{obsolete_inline}}</s></dt> + <dd><s class="obsoleteElement">Evaluava un string de codi font JavaScript dins el context de l'objecte especificat. S'ha eliminat.</s></dd> +</dl> + +<h2 id="Exemples">Exemples</h2> + +<p>Degut a que JavaScript no res semblant a subclasses d'objectes, la propietat <code>prototype</code> és una bona forma d'utilitzar algunes funcions que fan d'objectes com a "classe base". Per exemple:</p> + +<pre class="brush: js">var Person = function() { + this.canTalk = true; +}; + +Person.prototype.greet = function() { + if (this.canTalk) { + console.log('Hi, I am ' + this.name); + } +}; + +var Employee = function(name, title) { + Person.call(this); + this.name = name; + this.title = title; +}; + +Employee.prototype = Object.create(Person.prototype); +Employee.prototype.constructor = Employee; + +Employee.prototype.greet = function() { + if (this.canTalk) { + console.log('Hi, I am ' + this.name + ', the ' + this.title); + } +}; + +var Customer = function(name) { + Person.call(this); + this.name = name; +}; + +Customer.prototype = Object.create(Person.prototype); +Customer.prototype.constructor = Customer; + +var Mime = function(name) { + Person.call(this); + this.name = name; + this.canTalk = false; +}; + +Mime.prototype = Object.create(Person.prototype); +Mime.prototype.constructor = Mime; + +var bob = new Employee('Bob', 'Builder'); +var joe = new Customer('Joe'); +var rg = new Employee('Red Green', 'Handyman'); +var mike = new Customer('Mike'); +var mime = new Mime('Mime'); + +bob.greet(); +// Hi, I am Bob, the Builder + +joe.greet(); +// Hi, I am Joe + +rg.greet(); +// Hi, I am Red Green, the Handyman + +mike.greet(); +// Hi, I am Mike + +mime.greet(); +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Definició inicial. Implementat a JavaScript 1.0.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.2.3.1', 'Object.prototype')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-object.prototype', 'Object.prototype')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també"> Vegeu també</h2> + +<ul> + <li><a href="/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript">Introducció al JavaScript orientat a objectes</a></li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakmap/clear/index.html b/files/ca/web/javascript/reference/global_objects/weakmap/clear/index.html new file mode 100644 index 0000000000..443e075d46 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakmap/clear/index.html @@ -0,0 +1,91 @@ +--- +title: WeakMap.prototype.clear() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/clear +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/clear +--- +<div>{{JSRef}} {{obsolete_header}}</div> + +<p>El mètode <code><strong>clear()</strong></code> elimina tots els elements d'un objecte <code>WeakMap.</code></p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>wm</em>.clear();</code></pre> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_clear">Utilitzar el mètode <code>clear</code></h3> + +<pre class="brush: js;highlight:[10]">var wm = new WeakMap(); +var obj = {}; + +wm.set(obj, "foo"); +wm.set(window, "bar"); + +wm.has(obj); // true +wm.has(window); // true + +wm.clear(); + +wm.has(obj) // false +wm.has(window) // false +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<p>No forma part de cap especificació o borrador actual. Aquest mètode formava part del borrador de l'especificació d'ECMAScript 6 fins a la revisió número 28 (versió del 14 d'octubre del 2014), però s'ha eliminat en versions posteriors del borrador. No formarà part de l'estàndard final.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop("20.0")}}</td> + <td>11</td> + <td>23</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("20.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakmap/delete/index.html b/files/ca/web/javascript/reference/global_objects/weakmap/delete/index.html new file mode 100644 index 0000000000..1037fdb3fc --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakmap/delete/index.html @@ -0,0 +1,114 @@ +--- +title: WeakMap.prototype.delete() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/delete +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/delete +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>delete()</strong></code> elimina l'element especificat d'un objecte <code>WeakMap.</code></p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>wm</em>.delete(key);</code></pre> + +<h3 id="Parameters">Parameters</h3> + +<dl> + <dt>key</dt> + <dd>Necessari. La clau de l'element a eliminar de l'objecte <code>WeakMap.</code></dd> +</dl> + +<h3 id="Valor_de_retorn">Valor de retorn</h3> + +<p>Retorna <code>true</code> si un element en l'objecte <code>WeakMap</code> s'ha eliminat satisfactòriament.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzarel_mètode_delete">Utilitzarel mètode <code>delete</code></h3> + +<pre class="brush: js;highlight:[4]">var wm = new WeakMap(); +wm.set(window, "foo"); + +wm.delete(window); // Retorna true. Eliminat satisfactòriament + +wm.has(window); // Retorna false. L'objecte window ja no es troba en el WeakMap. +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakmap.prototype.delete', 'WeakMap.prototype.delete')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Notes_específiques_per_a_Firefox">Notes específiques per a Firefox</h2> + +<ul> + <li>Abans de SpiderMonkey 38 {{geckoRelease(38)}}, aquest mètode llançava un {{jsxref("TypeError")}} quan el paràmetre <code>key</code> no era un objecte. Aquest error s'ha arreglat en la versió 38 i posteriors al retornar <code>false</code> segons l'últim estàndard ES6 ({{bug(1127827)}}).</li> +</ul> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakmap/get/index.html b/files/ca/web/javascript/reference/global_objects/weakmap/get/index.html new file mode 100644 index 0000000000..f69ca1aa58 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakmap/get/index.html @@ -0,0 +1,115 @@ +--- +title: WeakMap.prototype.get() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/get +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/get +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>get()</strong></code> retorna un element especificat d'un objecte <code>WeakMap.</code></p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>wm</em>.get(key);</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt>key</dt> + <dd>Necessari. La clau d'un element a retornar de l'objecte <code>WeakMap.</code></dd> +</dl> + +<h3 id="Valor_a_retornar">Valor a retornar</h3> + +<p>Retorna l'element associat a la clau especificada o <code>undefined</code> si no es pot trobar la clau en l'objecte <code>WeakMap.</code></p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_get">Utilitzar el mètode <code>get</code></h3> + +<pre class="brush: js">var wm = new WeakMap(); +wm.set(window, "foo"); + +wm.get(window); // Retorna "foo". +wm.get("baz"); // Retorna undefined. +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakmap.prototype.get', 'WeakMap.prototype.get')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Notes_específiques_per_a_Firefox">Notes específiques per a Firefox</h2> + +<ul> + <li>Abans de SpiderMonkey 38 {{geckoRelease(38)}}, aquest mètode llençava un {{jsxref("TypeError")}} quan no es trobava el paràmetre key en l'objecte. Tot i així, l'últim estàndard ES6 especifica que en comptes d'això es retorni <code>undefined.</code> És més, <code>WeakMap.prototype.get</code> acceptava un segon argument opcional com a valor com a pla B, el qual no forma part de l'estàndard. Ambdós comportaments no estàndards s'han eliminat en la versió 38 i posterior ({{bug(1127827)}}).</li> +</ul> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakMap.set()")}}</li> + <li>{{jsxref("WeakMap.has()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakmap/has/index.html b/files/ca/web/javascript/reference/global_objects/weakmap/has/index.html new file mode 100644 index 0000000000..0a2d017462 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakmap/has/index.html @@ -0,0 +1,118 @@ +--- +title: WeakMap.prototype.has() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/has +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/has +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>has()</strong></code> retorna un booleà indicant si un element amb una clau especificada existeix o no en l'objecte <code>WeakMap.</code></p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>wm</em>.has(key);</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt>key</dt> + <dd>Necessari. La clau de l'element a comprovar la seva presència de l'objecte <code>WeakMap.</code></dd> +</dl> + +<h3 id="valor_de_retorn">valor de retorn</h3> + +<dl> + <dt>Booleà</dt> + <dd>Retorna <code>true</code> si un element amb una clau especificada existeix en l'objecte <code>WeakMap</code>; en el cas contrari retornarà <code>false</code>.</dd> +</dl> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_has">Utilitzar el mètode <code>has</code></h3> + +<pre class="brush: js">var wm = new WeakMap(); +wm.set(window, "foo"); + +wm.has(window); // retorna true +wm.has("baz"); // retorna false +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakmap.prototype.has', 'WeakMap.prototype.has')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Notes_específiques_per_a_Firefox">Notes específiques per a Firefox</h2> + +<ul> + <li>Abans de SpiderMonkey 38 {{geckoRelease(38)}}, aquest mètode llençava un {{jsxref("TypeError")}} quan el paràmetre <code>key</code> no era un objecte. Això s'ha arreglat en la versió 38 i posteriors per tal de retornar <code>false</code> segons l'últim estàndard ES6 ({{bug(1127827)}}).</li> +</ul> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakMap.prototype.set()")}}</li> + <li>{{jsxref("WeakMap.prototype.get()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakmap/index.html b/files/ca/web/javascript/reference/global_objects/weakmap/index.html new file mode 100644 index 0000000000..27fdd6a71d --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakmap/index.html @@ -0,0 +1,279 @@ +--- +title: WeakMap +slug: Web/JavaScript/Reference/Global_Objects/WeakMap +tags: + - ECMAScript6 + - Experimental + - JavaScript + - NeedsTranslation + - TopicStub + - WeakMap +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap +--- +<div>{{JSRef}}</div> + +<p>L'objecte <strong><code>WeakMap</code></strong> és una col·lecció de parelles clau/valor on les claus són dèbilment referenciades. Les claus han de ser objectes i els valors poden ser valors aribitraris.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox notranslate"><code>new WeakMap([iterable]) +</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt><code>iterable</code></dt> + <dd>Iterable és un Array o un altre objecte iterable el qual els seus elements són parelles clau/valor (Array de 2 elements). Cada parella clau/valor serà afegida al nou WeakMap. null és tractat com a undefined.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>Les claus de WeakMaps són només de tipus <code>Object.</code> {{Glossary("Primitive", "Primitive data types")}} com a claus no són permesos (e.g. a {{jsxref("Symbol")}} no pot ser una clau <code>WeakMap</code>).</p> + +<p>La clau d'un WeakMap es sostè dèbilment. El que significa que, si no hi ha altres referències fortes a la clau, llavors la entrada sencera serà eliminada del WeakMap pel recol·lector de brossa (garbage collector).</p> + +<h3 id="Per_què_WeakMap">Per què <em>Weak</em>Map?</h3> + +<p>El programador expert en JavaScript s'adonarà que aquesta API es podria implementar en JavaScript amb dos arrays (una per a claus, i una per valors) compartides pels quatre mètodes de l'API. Tal implementació tindria dos inconvenients principals. El primer és que la cerca té un cost de O(n) (on n és el nombre de claus al mapa). El segon és que té problemes de fuita de memòria (memory leak). Amb els mapes mantinguts manualment, l'array de claus mantindria referències a le objectes clau, evitant que aquests fóssin eliminats de memòria pel recol·lector de brossa. Als WeakMaps natius, les referències als objectes clau són "dèbils", que vol dir que el recol·lector de brossa pot eliminar l'objecte de memòria si aquest només és referenciat per referències dèbils.</p> + +<p>Degut a que les referències són dèbils les claus del WeakMap no són enumerables (és a dir, no hi ha cap mètode que us retornarà un llistat de claus). Si aquest mètode existís, aquest dependria de l'estat del recol·lector de brossa, introduïnt un comportament no determinista. Si voleu tenir un llistat amb les claus, l'haureu de mantenir pel vostre compte.</p> + +<h2 id="Propietats">Propietats</h2> + +<dl> + <dt><code>WeakMap.length</code></dt> + <dd>El valor de la propietat <code>length</code> és 0.</dd> + <dt>{{jsxref("WeakMap.prototype")}}</dt> + <dd>Representa el prototip pel constructor <code>WeakMap.</code> Permet l'adició de propietats a tots els objectes <code>WeakMap.</code></dd> +</dl> + +<h2 id="Instàncies_WeakMap">Instàncies <code>WeakMap</code></h2> + +<p>Totes les instàncies <code>WeakMap</code> hereten de {{jsxref("WeakMap.prototype")}}.</p> + +<h3 id="Propietats_2">Propietats</h3> + +<p>{{page('ca/Web/JavaScript/Reference/Global_Objects/WeakMap/prototype','Properties')}}</p> + +<h3 id="Mètodes">Mètodes</h3> + +<p>{{page('ca/Web/JavaScript/Reference/Global_Objects/WeakMap/prototype','Methods')}}</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_WeakMap">Utilitzar <code>WeakMap</code></h3> + +<pre class="brush: js notranslate">var wm1 = new WeakMap(), + wm2 = new WeakMap(), + wm3 = new WeakMap(); +var o1 = {}, + o2 = function(){}, + o3 = window; + +wm1.set(o1, 37); +wm1.set(o2, "azerty"); +wm2.set(o1, o2); // un valor pot ser qualsevol cosa, incloent un objecte object o una funció +wm2.set(o3, undefined); +wm2.set(wm1, wm2); // claus i valors poden ser qualsevol objecte. Fins i tot WeakMaps! + +wm1.get(o2); // "azerty" +wm2.get(o2); // undefined, ja que no hi ha cap valor per o2 a wm2 +wm2.get(o3); // undefined, al tenir assignat aquest valor + +wm1.has(o2); // true +wm2.has(o2); // false +wm2.has(o3); // true (encara que el valor en si sigui 'undefined') + +wm3.set(o1, 37); +wm3.get(o1); // 37 + +wm1.has(o1); // true +wm1.delete(o1); +wm1.has(o1); // false +</pre> + +<h3 id="Implementar_una_classe_tipus_WeakMap_amb_un_mètode_.clear">Implementar una classe tipus <code>WeakMap</code> amb un mètode .clear()</h3> + +<p>Amb fins expositius, l'exemple següent utilitza el nou constructor <code>class</code> d' ECMAScript 6, el qual no està àmpliament implementat.</p> + +<pre class="brush: js notranslate">class ClearableWeakMap { + constructor(init) { + this._wm = new WeakMap(init) + } + clear() { + this._wm = new WeakMap() + } + delete(k) { + return this._wm.delete(k) + } + get(k) { + return this._wm.get(k) + } + has(k) { + return this._wm.has(k) + } + set(k, v) { + this._wm.set(k, v) + return this + } +} +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ES6', '#sec-weakmap-objects', 'WeakMap')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>{{ CompatOpera(23) }}</td> + <td>7.1</td> + </tr> + <tr> + <td><code>new WeakMap(iterable)</code></td> + <td>38</td> + <td>{{CompatGeckoDesktop("36")}}</td> + <td>{{CompatNo}}</td> + <td>{{ CompatOpera(25) }}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>clear()</code></td> + <td>36</td> + <td>{{CompatGeckoDesktop("20.0")}}</td> + <td>11</td> + <td>{{ CompatOpera(23) }}</td> + <td>7.1</td> + </tr> + <tr> + <td>Constructor argument: <code>new WeakMap(null)</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("37")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>Monkey-patched <code>set()</code> in constructor</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("37")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>WeakMap()</code> without <code>new</code> throws</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("42")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>35</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + <tr> + <td><code>new WeakMap(iterable)</code></td> + <td>38</td> + <td>{{CompatGeckoMobile("36")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td><code>clear()</code></td> + <td>35</td> + <td>{{CompatGeckoMobile("20.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + <tr> + <td>Constructor argument: <code>new WeakMap(null)</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("37")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>Monkey-patched <code>set()</code> in constructor</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("37")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>WeakMap()</code> without <code>new</code> throws</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("42")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li><a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=547941">Bug de WeakMap a Mozilla</a></li> + <li><a href="http://fitzgeraldnick.com/weblog/53/">Amagant detalls d'implementació amb els WeakMaps de l'ECMAScript 6</a></li> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("WeakSet")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakmap/prototype/index.html b/files/ca/web/javascript/reference/global_objects/weakmap/prototype/index.html new file mode 100644 index 0000000000..5155a0a664 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakmap/prototype/index.html @@ -0,0 +1,132 @@ +--- +title: WeakMap.prototype +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap +--- +<div>{{JSRef}}</div> + +<p>La propietat <code><strong>WeakMap</strong></code><strong><code>.prototype</code></strong> representa el prototip pel constructor {{jsxref("WeakMap")}}.</p> + +<div>{{js_property_attributes(0,0,0)}}</div> + +<h2 id="Descripció">Descripció</h2> + +<p>Les instantànies {{jsxref("WeakMap")}} hereten de {{jsxref("WeakMap.prototype")}}. Es pot utilitzar l'objecte prototip del constructor per afegir propietats o mètodes a totes les instàncies <code>WeakMap.</code></p> + +<p><code>WeakMap.prototype</code> és en si mateix només un objecte ordinari:</p> + +<pre class="brush: js">Object.prototype.toString.call(WeakMap.prototype); // "[object Object]"</pre> + +<h2 id="Propietats">Propietats</h2> + +<dl> + <dt><code>WeakMap.prototype.constructor</code></dt> + <dd>Retorna la funció que ha creat un prototip de la instància. Aquesta és la funció {{jsxref("WeakMap")}} per defecte.</dd> +</dl> + +<h2 id="Mètodes">Mètodes</h2> + +<dl> + <dt>{{jsxref("WeakMap.delete", "WeakMap.prototype.delete(key)")}}</dt> + <dd>Elimina qualsevol valor associat a <code>key</code>. <code>WeakMap.prototype.has(key)</code> retornarà <code>false</code> després d'això.</dd> + <dt>{{jsxref("WeakMap.get", "WeakMap.prototype.get(key)")}}</dt> + <dd>Retorna el valor associat a <code>key</code>, o <code>undefined</code> si no n'hi ha cap.</dd> + <dt>{{jsxref("WeakMap.has", "WeakMap.prototype.has(key)")}}</dt> + <dd>Retorna un booleà afirmant si un valor s'ha associat o no a la <code>key</code> en l'objecte <code>WeakMap</code>.</dd> + <dt>{{jsxref("WeakMap.set", "WeakMap.prototype.set(key, value)")}}</dt> + <dd>Estableix el valor per la <code>key</code> en l'objecte <code>WeakMap.</code> Retorna l'objecte <code>WeakMap.</code></dd> + <dt><s class="obsoleteElement">{{jsxref("WeakMap.prototype.clear()")}} {{obsolete_inline}}</s></dt> + <dd><s class="obsoleteElement">Elimina totes les parelles clau/valor de l'objecte <code>WeakMap.</code> Vegeu que es posible implementar un objecte <code>WeakMap</code>-like que tingui un mètode <code>.clear()</code> per mitjà d'encapsular un objecte <code>WeakMap </code>que no ho tingui (vegeu un exemple a la pàgina {{jsxref("WeakMap")}})</s></dd> +</dl> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakmap.prototype', 'WeakMap.prototype')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</td> + <td>7.1</td> + </tr> + <tr> + <td>Objecte ordinari</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop("40")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Chrome per Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + <tr> + <td>Objecte ordinari</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("40")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Map.prototype")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakmap/set/index.html b/files/ca/web/javascript/reference/global_objects/weakmap/set/index.html new file mode 100644 index 0000000000..800a097971 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakmap/set/index.html @@ -0,0 +1,120 @@ +--- +title: WeakMap.prototype.set() +slug: Web/JavaScript/Reference/Global_Objects/WeakMap/set +translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/set +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>set()</strong></code> afegeix un nou element amb una <code>key</code> i un <code>value</code> especificats a un objecte <code>WeakMap</code>.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>wm</em>.set(key, value);</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt>key</dt> + <dd>Necesari. La clau de l'element a afegir al objecte <code>WeakMap.</code></dd> + <dt>value</dt> + <dd>Necesari. El valor de l'element a afegir a l'objecte <code>WeakMap.</code></dd> +</dl> + +<h3 id="Valor_de_retorn">Valor de retorn</h3> + +<p>L'objecte <code>WeakMap</code>.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_set">Utilitzar el mètode <code>set</code></h3> + +<pre class="brush: js">var wm = new WeakMap(); +var obj = {}; + +// Afegir nous elements al WeakMap +wm.set(obj, "foo").set(window, "bar"); // chainable + +// Actualitzem un element en WeakMap +wm.set(obj, "baz"); +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakmap.prototype.set', 'WeakMap.prototype.set')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop("6.0")}}</td> + <td>11</td> + <td>23</td> + <td>7.1</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile("6.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>8</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Notes_específiques_per_a_Firefox">Notes específiques per a Firefox</h2> + +<ul> + <li>Abans de Firefox 33 {{geckoRelease("33")}}, <code>WeakMap.prototype.set</code> retornava <code>undefined</code> al no poder-se encadenar. Això s'ha arreglat ({{bug(1031632)}}). El comportament es pot trobar també a Chrome/v8 (<a href="https://code.google.com/p/v8/issues/detail?id=3410">problema</a>).</li> +</ul> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakMap")}}</li> + <li>{{jsxref("WeakMap.prototype.get()")}}</li> + <li>{{jsxref("WeakMap.prototype.has()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakset/add/index.html b/files/ca/web/javascript/reference/global_objects/weakset/add/index.html new file mode 100644 index 0000000000..983b24329a --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakset/add/index.html @@ -0,0 +1,103 @@ +--- +title: WeakSet.prototype.add() +slug: Web/JavaScript/Reference/Global_Objects/WeakSet/add +translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet/add +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>add()</strong></code> afegeix un nou objecte al final d'un objecte <code>WeakSet</code>.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>ws</em>.add(value);</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt>value</dt> + <dd>Necessari. L'objecte que es vol afegir a la col·lecció de <code>WeakSet.</code></dd> +</dl> + +<h2 id="Utilitzar_el_mètode_add">Utilitzar el mètode <code>add</code></h2> + +<pre class="brush: js">var ws = new WeakSet(); + +ws.add(window); // afegeix l'objecte window al WeakSet + +ws.has(window); // true +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakset.prototype.add', 'WeakSet.prototype.add')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop(34)}}</td> + <td>{{CompatNo}}</td> + <td>23</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{ CompatGeckoMobile(34) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakSet")}}</li> + <li>{{jsxref("WeakSet.prototype.delete()")}}</li> + <li>{{jsxref("WeakSet.prototype.has()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakset/clear/index.html b/files/ca/web/javascript/reference/global_objects/weakset/clear/index.html new file mode 100644 index 0000000000..4e6b2e6f8c --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakset/clear/index.html @@ -0,0 +1,87 @@ +--- +title: WeakSet.prototype.clear() +slug: Web/JavaScript/Reference/Global_Objects/WeakSet/clear +translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet/clear +--- +<div>{{JSRef}} {{obsolete_header}}</div> + +<p>El mètode <code><strong>clear()</strong></code> elimina tots els elements d'un objecte <code>WeakSet.</code></p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>ws</em>.clear();</code></pre> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_clear">Utilitzar el mètode <code>clear</code></h3> + +<pre class="brush: js">var ws = new WeakSet(); + +ws.add(window); +ws.has(window); // true + +ws.clear(); + +ws.has(window); // false +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<p>No forma part de cap especificació actual o borrador. Aquest mètode formava part del borrador de l'especificació d'ECMAScript 6 fins la revisió 28 (versió del 14 d'octubre del 2014), però s'ha eliminat en versions posteriors del borrador. No formarà part de l'estàndard final.</p> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop(34)}}</td> + <td>{{CompatNo}}</td> + <td>23</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoMobile(34)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakSet")}}</li> + <li>{{jsxref("WeakSet.prototype.delete()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakset/delete/index.html b/files/ca/web/javascript/reference/global_objects/weakset/delete/index.html new file mode 100644 index 0000000000..9bfaa0f439 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakset/delete/index.html @@ -0,0 +1,112 @@ +--- +title: WeakSet.prototype.delete() +slug: Web/JavaScript/Reference/Global_Objects/WeakSet/delete +translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet/delete +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>delete()</strong></code>elimina l'element especificat de l'objecte <code>WeakSet.</code></p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>ws</em>.delete(value);</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt>value</dt> + <dd>Necessari. L'objecte que es vol eliminar de l'objecte <code>WeakSet.</code></dd> +</dl> + +<h3 id="Return_value">Return value</h3> + +<p>Retorna <code>true</code> si l'element dins l'objecte <code>WeakSet</code> s'ha eliminat satisfactòriament; Del cas contrari retornarà <code>false</code>.</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_delete">Utilitzar el mètode <code>delete</code></h3> + +<pre class="brush: js">var ws = new WeakSet(); +var obj = {}; + +ws.add(window); + +ws.delete(obj); // Retorna false. No s'ha trobat cap obj per eliminar. +ws.delete(window); // Retorna true. Eliminat satisfactòriament. + +ws.has(window); // Retorna false. window ja no és present en WeakSet. +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakset.prototype.delete', 'WeakSet.prototype.delete')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop(34)}}</td> + <td>{{CompatNo}}</td> + <td>23</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{ CompatGeckoMobile(34) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakSet")}}</li> + <li>{{jsxref("WeakSet.prototype.clear()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakset/has/index.html b/files/ca/web/javascript/reference/global_objects/weakset/has/index.html new file mode 100644 index 0000000000..b2074a9847 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakset/has/index.html @@ -0,0 +1,113 @@ +--- +title: WeakSet.prototype.has() +slug: Web/JavaScript/Reference/Global_Objects/WeakSet/has +translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet/has +--- +<div>{{JSRef}}</div> + +<p>El mètode <code><strong>has()</strong></code> retorna un booleà indicant si un objecte existeix o no en unmethod returns a boolean indicating whether an object exists in a <code>WeakSet</code> or not.</p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"><code><em>ws</em>.has(valor);</code></pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt>valor</dt> + <dd>Necessari. L'objecte a comprovar la seva presència en <code>WeakSet</code>.</dd> +</dl> + +<h3 id="Valor_de_retorn">Valor de retorn</h3> + +<dl> + <dt>Booleà</dt> + <dd>Retorna <code>true</code> si un element amb el valor especificat existeix en l'objecte <code>WeakSet</code>; en el cas contrari retornarà <code>false</code>.</dd> +</dl> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_el_mètode_has">Utilitzar el mètode <code>has</code></h3> + +<pre class="brush: js">var ws = new WeakSet(); +var obj = {}; +ws.add(window); + +mySet.has(window); // retorna true +mySet.has(obj); // retorna false +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakset.prototype.has', 'WeakSet.prototype.has')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{CompatGeckoDesktop(34)}}</td> + <td>{{CompatNo}}</td> + <td>23</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{ CompatGeckoMobile(34) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("WeakSet")}}</li> + <li>{{jsxref("WeakSet.prototype.add()")}}</li> + <li>{{jsxref("WeakSet.prototype.delete()")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakset/index.html b/files/ca/web/javascript/reference/global_objects/weakset/index.html new file mode 100644 index 0000000000..d1ae3999a6 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakset/index.html @@ -0,0 +1,201 @@ +--- +title: WeakSet +slug: Web/JavaScript/Reference/Global_Objects/WeakSet +tags: + - ECMAScript6 + - Experimental + - JavaScript + - NeedsTranslation + - TopicStub + - WeakSet +translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet +--- +<div>{{JSRef}}</div> + +<p><u>L'objecte <strong><code>WeakSet</code></strong> permet emmagatzemar dèbilment <em>objects</em> en una col·lecció.</u></p> + +<h2 id="Sintaxi">Sintaxi</h2> + +<pre class="syntaxbox"> new WeakSet([iterable]);</pre> + +<h3 id="Paràmetres">Paràmetres</h3> + +<dl> + <dt>iterable</dt> + <dd>Si es pasa un <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...of">objecte iterable</a>, tots els seus elements seràn afegits al nou <code>WeakSet</code>. null es tractat com a undefined.</dd> +</dl> + +<h2 id="Descripció">Descripció</h2> + +<p>Els objectes <code>WeakSet</code> són col·leccions d'objectes. Un objecte al <code>WeakSet</code> només pot passar un cop, és únic en la col·lecció de <code>WeakSet.</code></p> + +<p>Les principals diferències respecte l'objecte {{jsxref("Set")}} són:</p> + +<ul> + <li>Al contrari que <code>Sets</code>, <code>WeakSets</code> són <strong>únicament col·leccions d'objectes</strong> i no de valors arbitraris de qualsevol tipus.</li> + <li> <code>WeakSet</code> és <em>dèbil (weak)</em>: Les referències a la col·lecció es mantenen dèbilment. SI no hi ha cap altra referència a un objecte emmagatzemat en <code>WeakSet</code>,<u> poden ser recollits com a brossa</u>. Això també vol dir que no hi ha cap llista d'objectes actuals emmagatzemats a la col·lecció. <code>WeakSets</code> no són enumerables.</li> +</ul> + +<h2 id="Propietats">Propietats</h2> + +<dl> + <dt><code>WeakSet.length</code></dt> + <dd>El valor de la propietat <code>length</code> és 0.</dd> + <dt>{{jsxref("WeakSet.prototype")}}</dt> + <dd>Representa el prototip per al constructor de <code>Set.</code> Permet afegir propietats a tots els objectes <code>WeakSet</code>.</dd> +</dl> + +<h2 id="Instàncies_WeakSet">Instàncies <code>WeakSet</code></h2> + +<p>Totes les instàncies <code>WeakSet</code> hereten de {{jsxref("WeakSet.prototype")}}.</p> + +<h3 id="Propietats_2">Propietats</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Properties')}}</p> + +<h3 id="Mètodes">Mètodes</h3> + +<p>{{page('en-US/Web/JavaScript/Reference/Global_Objects/WeakSet/prototype','Methods')}}</p> + +<h2 id="Exemples">Exemples</h2> + +<h3 id="Utilitzar_l'objecte_WeakSet">Utilitzar l'objecte <code>WeakSet</code></h3> + +<pre class="brush: js">var ws = new WeakSet(); +var obj = {}; +var foo = {}; + +ws.add(window); +ws.add(obj); + +ws.has(window); // true +ws.has(foo); // false, foo no s'ha afegit al conjunt + +ws.delete(window); // elimina window del conjunt +ws.has(window); // false, window ha sigut eliminat + +ws.clear(); // buida tot el WeakSet +</pre> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakset-objects', 'WeakSet')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatChrome(36)}}</td> + <td>{{ CompatGeckoDesktop(34) }}</td> + <td>{{CompatNo}}</td> + <td>{{ CompatOpera(23) }}</td> + <td>9</td> + </tr> + <tr> + <td><code>new WeakSet(iterable)</code></td> + <td>38</td> + <td>{{ CompatGeckoDesktop(34) }}</td> + <td>{{CompatNo}}</td> + <td>25</td> + <td>9</td> + </tr> + <tr> + <td>Constructor argument: <code>new WeakSet(null)</code></td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("37")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>9</td> + </tr> + <tr> + <td>Monkey-patched <code>add()</code> in Constructor</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("37")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>9</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{ CompatGeckoMobile(34) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>9</td> + </tr> + <tr> + <td><code>new WeakMap(iterable)</code></td> + <td>{{CompatNo}}</td> + <td>{{ CompatGeckoMobile(34) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>9</td> + </tr> + <tr> + <td>Constructor argument: <code>new WeakSet(null)</code></td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>9</td> + </tr> + <tr> + <td>Monkey-patched <code>add()</code> in Constructor</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>9</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Map")}}</li> + <li>{{jsxref("Set")}}</li> + <li>{{jsxref("WeakMap")}}</li> +</ul> diff --git a/files/ca/web/javascript/reference/global_objects/weakset/prototype/index.html b/files/ca/web/javascript/reference/global_objects/weakset/prototype/index.html new file mode 100644 index 0000000000..2b4cd51048 --- /dev/null +++ b/files/ca/web/javascript/reference/global_objects/weakset/prototype/index.html @@ -0,0 +1,131 @@ +--- +title: WeakSet.prototype +slug: Web/JavaScript/Reference/Global_Objects/WeakSet/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/WeakSet +--- +<div>{{JSRef}}</div> + +<p>La propietat <code><strong>WeakSet</strong></code><strong><code>.prototype</code></strong> representa el prototip pel constructor {{jsxref("WeakSet")}}.</p> + +<div>{{js_property_attributes(0,0,0)}}</div> + +<h2 id="Descripció">Descripció</h2> + +<p>Les instàncies {{jsxref("WeakSet")}} hereten de {{jsxref("WeakSet.prototype")}}. Es pot utilitzar l'objecte prototip del constructor per afegir propietats o mètodes a totes les instàncies <code>WeakSet</code>.</p> + +<p><code>WeakSet.prototype</code> és en si mateix només un objecte ordinari</p> + +<pre class="brush: js">Object.prototype.toString.call(WeakSet.prototype); // "[object Object]"</pre> + +<h2 id="Propietats">Propietats</h2> + +<dl> + <dt><code>WeakSet.prototype.constructor</code></dt> + <dd>Retorna la funció que ha creat un prototip de la instància. Aquesta és la funció {{jsxref("WeakSet")}} per defecte.</dd> +</dl> + +<h2 id="Mètodes">Mètodes</h2> + +<dl> + <dt>{{jsxref("WeakSet.add", "WeakSet.prototype.add(value)")}}</dt> + <dd>Afegeix un nou element amb el valor donat a l'objecte <code>WeakSet.</code></dd> + <dt>{{jsxref("WeakSet.delete", "WeakSet.prototype.delete(value)")}}</dt> + <dd>Elimina l'element associat al <code>value</code>. <code>WeakSet.prototype.has(value)</code> retornarà després <code>false</code>.</dd> + <dt>{{jsxref("WeakSet.has", "WeakSet.prototype.has(value)")}}</dt> + <dd>Retorna un booleà afirmant si un element és o no present amb el valor donat en l'objecte <code>WeakSet</code>.</dd> + <dt><s class="obsoleteElement">{{jsxref("WeakSet.prototype.clear()")}} {{obsolete_inline}}</s></dt> + <dd><s class="obsoleteElement">Elimina tots els elements de l'objecte <code>WeakSet.</code></s></dd> +</dl> + +<h2 id="Especificacions">Especificacions</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificació</th> + <th scope="col">Estat</th> + <th scope="col">Comentaris</th> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-weakset.prototype', 'WeakSet.prototype')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Definició inicial.</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2> + +<p>{{CompatibilityTable}}</p> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>36</td> + <td>{{ CompatGeckoDesktop(34) }}</td> + <td>{{CompatNo}}</td> + <td>23</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td>Objecte ordinari</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop("40")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Característica</th> + <th>Chrome per Android</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suport bàsic</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{ CompatGeckoMobile(34) }}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + <tr> + <td>Objecte ordinari</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile("40")}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Vegeu_també">Vegeu també</h2> + +<ul> + <li>{{jsxref("Set.prototype")}}</li> + <li>{{jsxref("WeakMap.prototype")}}</li> +</ul> |