You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Just like the `in` operator, the `for in` loop also traverses the prototype
4
-
chain when iterating over the properties of an object.
3
+
Aivan kuten `in`-operaattori, myös `for in`-luuppi käy olion prototyyppiketjun läpi iteroidessaan sen ominaisuuksia.
5
4
6
-
> **Note:** The `for in` loop will **not** iterate over any properties that
7
-
> have their `enumerable` attribute set to `false`; for example, the `length`
8
-
> property of an array.
9
-
10
-
// Poisoning Object.prototype
5
+
> **Huomio:**`for in`-luuppi **ei** iteroi ominaisuuksia, joiden `enumerable`-attribuutti on asetettu arvoon `false`. Eräs esimerkki tästä on taulukon `length`-ominaisuus.
6
+
7
+
// Object.prototypen myrkyttäminen
11
8
Object.prototype.bar = 1;
12
9
13
10
var foo = {moo: 2};
14
11
for(var i in foo) {
15
-
console.log(i); // prints both bar and moo
12
+
console.log(i); // tulostaa sekä bar että moo
16
13
}
17
14
18
-
Since it is not possible to change the behavior of the `for in` loop itself, it
19
-
is necessary to filter out the unwanted properties inside the loop body ,
20
-
this is done by using the [`hasOwnProperty`](#object.hasownproperty) method of
21
-
`Object.prototype`.
15
+
Koska `for in`-luupin käytöstapaa ei voida muokata suoraan, tulee ei-halutut ominaisuudet karsia itse luupin sisällä. Tämä on mahdollista käyttäen `Object.prototype`-olion [`hasOwnProperty`](#object.hasownproperty)-metodia.
22
16
23
-
> **Note:** Since the `for in` always traverses the complete prototype chain, it
24
-
> will get slower with each additional layer of inheritance added to an object.
17
+
> **Huomio:**`for in`-luupin suorittaminen hidastuu sitä enemmän, mitä pidempi olion prototyyppiketju on. Tämä johtuu siitä, että se joutuu käymään koko ketjun sisällön läpi.
This version is the only correct one to use. Due to the use of `hasOwnProperty` it
36
-
will **only** print out `moo`. When `hasOwnProperty` is left out, the code is
37
-
prone to errors in cases where the native prototypes - e.g. `Object.prototype` -
38
-
have been extended.
28
+
Tämä versio on ainut oikea. Se tulostaa **ainoastaan**`moo`, koska se käyttää `hasOwnProperty`-metodia oikein. Kun se jätetään pois, on koodi altis virheille tapauksissa, joissa prototyyppejä, kuten `Object.prototype`, on laajennettu.
39
29
40
-
One widely used framework which does this is [Prototype][1]. When this
41
-
framework is included, `for in` loops that do not use `hasOwnProperty` are
42
-
guaranteed to break.
30
+
[Prototype][1] on eräs yleisesti käytetty ohjelmointialusta, joka tekee näin. Kun kyseistä alustaa käytetään, `for in`-luupit, jotka eivät käytä `hasOwnProperty`-metodia, menevät varmasti rikki.
43
31
44
-
### In Conclusion
32
+
### Yhteenveto
45
33
46
-
It is recommended to **always** use `hasOwnProperty`. Never should any
47
-
assumptions be made about the environment the code is running in, or whether the
48
-
native prototypes have been extended or not.
34
+
On suositeltavaa käyttää **aina**`hasOwnProperty`-metodia. Ei ole kannattavaa tehdä ajoympäristöön tai prototyyppeihin liittyviä oletuksia.
Everything in JavaScript acts like an object, with the only two exceptions being
4
-
[`null`](#core.undefined) and [`undefined`](#core.undefined).
3
+
Kaikki muuttujat, kahta poikkeusta lukuunottamatta, käyttäytyvät JavaScriptissä kuten olio. Nämä poikkeukset ovat [`null`](#core.undefined) sekä [`undefined`](#core.undefined).
5
4
6
5
false.toString() // 'false'
7
6
[1, 2, 3].toString(); // '1,2,3'
@@ -10,38 +9,31 @@ Everything in JavaScript acts like an object, with the only two exceptions being
10
9
Foo.bar = 1;
11
10
Foo.bar; // 1
12
11
13
-
A common misconception is that number literals cannot be used as
14
-
objects. That is because a flaw in JavaScript's parser tries to parse the *dot
15
-
notation* on a number as a floating point literal.
12
+
Yleisesti luullaan ettei numeroliteraaleja voida käyttää olioina. Tämä johtuu viasta JavaScriptin parserissa. Se yrittää parsia numeron *pistenotaatiota* liukulukuliteraalina.
16
13
17
-
2.toString(); // raises SyntaxError
14
+
2.toString(); // palauttaa SyntaxError-virheen
18
15
19
-
There are a couple of workarounds which can be used in order make number
20
-
literals act as objects too.
16
+
Tämä voidaan välttää esimerkiksi seuraavasti.
21
17
22
-
2..toString(); // the second point is correctly recognized
23
-
2 .toString(); // note the space left to the dot
24
-
(2).toString(); // 2 is evaluated first
18
+
2..toString(); // toinen piste tunnistuu oikein
19
+
2 .toString(); // huomaa pisteen vasemmalla puolen oleva väli
20
+
(2).toString(); // 2 arvioidaan ensi
25
21
26
-
### Objects as a Data Type
22
+
### Oliot tietotyyppinä
27
23
28
-
Objects in JavaScript can also be used as a [*Hashmap*][1], they mainly consist
29
-
of named properties mapping to values.
24
+
JavaScriptin olioita voidaan käyttää myös [*hajautustauluna*][1], koska ne muodostavat pääasiassa avaimien ja niihin liittyvien arvojen välisen mappauksen.
30
25
31
-
Using a object literal - `{}` notation - it is possible to create a
32
-
plain object. This new object [inherits](#object.prototype) from `Object.prototype` and
33
-
has no [own properties](#object.hasownproperty) defined on it.
26
+
Olioliteraalinotaatiota - `{}` - käyttäen voidaan luoda tyhjä olio. Tämä olio [perii](#object.prototype)`Object.prototype`-olion eikä sille ole määritelty [omia ominaisuuksia](#object.hasownproperty).
34
27
35
-
var foo = {}; // a new empty object
28
+
var foo = {}; // uusi, tyhjä olio
36
29
37
-
// a new object with a property called 'test' with value 12
30
+
// uusi, tyhjä olio, joka sisältää ominaisuuden 'test' arvolla 12
38
31
var bar = {test: 12};
39
32
40
-
### Accessing Properties
33
+
### Pääsy ominaisuuksiin
34
+
35
+
Olion ominaisuuksiin voidaan päästä käsiksi kahta eri tapaa käyttäen. Siihen voidaan käyttää joko piste- tai hakasulkunotaatiota.
41
36
42
-
The properties of an object can be accessed in two ways, via either the dot
43
-
notation, or the square bracket notation.
44
-
45
37
var foo = {name: 'Kitten'}
46
38
foo.name; // kitten
47
39
foo['name']; // kitten
@@ -50,17 +42,13 @@ notation, or the square bracket notation.
50
42
foo[get]; // kitten
51
43
52
44
foo.1234; // SyntaxError
53
-
foo['1234']; // works
45
+
foo['1234']; // toimii
54
46
55
-
Both notations are identical in their workings, with the only difference being that
56
-
the square bracket notation allows for dynamic setting of properties, as well as
57
-
the use of property names that would otherwise lead to a syntax error.
47
+
Kumpikin notaatio toimii samalla tavoin. Ainut ero liittyy siihen, että hakasulkunotaation avulla ominaisuuksien arvoja voidaan asettaa dynaamisesti. Se sallii myös muuten hankalien, virheeseen johtavien nimien käyttämisen.
58
48
59
-
### Deleting Properties
49
+
### Ominaisuuksien poistaminen
60
50
61
-
The only way to actually remove a property from an object is to use the `delete`
62
-
operator; setting the property to `undefined` or `null` only remove the
63
-
*value* associated with the property, but not the *key*.
51
+
Ainut tapa poistaa olion ominaisuus on käyttää `delete`-operaattoria. Ominaisuuden asettaminen joko arvoon `undefined` tai `null` poistaa vain siihen liittyneen arvon muttei itse *avainta*.
64
52
65
53
var obj = {
66
54
bar: 1,
@@ -77,23 +65,18 @@ operator; setting the property to `undefined` or `null` only remove the
77
65
}
78
66
}
79
67
80
-
The above outputs both `bar undefined` and `foo null` - only `baz` was
81
-
removed and is therefore missing from the output.
68
+
Yllä oleva koodi tulostaa sekä `both undefined` että `foo null`. Ainoastaan `baz` on poistettu. Täten sitä ei myöskään näy tulosteessa.
82
69
83
-
### Notation of Keys
70
+
### Avainnotaatio
84
71
85
72
var test = {
86
-
'case': 'I am a keyword so I must be notated as a string',
87
-
delete: 'I am a keyword too so me' // raises SyntaxError
73
+
'case': 'Olen avainsana, joten minun tulee olla merkkijono',
74
+
delete: 'Myös minä olen avainsana' // palauttaa SyntaxError-virheen
88
75
};
89
76
90
-
Object properties can be both notated as plain characters and as strings. Due to
91
-
another mis-design in JavaScript's parser, the above will throw
92
-
a `SyntaxError` prior to ECMAScript 5.
77
+
Olioiden ominaisuuksia voidaan notatoida käyttäen joko pelkkiä merkkejä tai merkkijonoja. Toisesta JavaScriptin suunnitteluvirheestä johtuen yllä oleva koodi palauttaa `SyntaxError`-virheen ECMAScript 5:ttä edeltävissä versioissa.
93
78
94
-
This error arises from the fact that `delete` is a *keyword*; therefore, it must be
95
-
notated as a *string literal* to ensure that it will be correctly interpreted by
96
-
older JavaScript engines.
79
+
Tämä virhe johtuu siitä, että `delete` on *avainsana*. Täten se tulee notatoida *merkkijonona*. Tällöin myös vanhemmat JavaScript-tulkit ymmärtävät sen oikein.
In order to check whether a object has a property defined on *itself* and **not**
4
-
somewhere on its [prototype chain](#object.prototype), it is necessary to use the
5
-
`hasOwnProperty` method which all objects inherit from `Object.prototype`.
3
+
Jotta voimme tarkistaa onko olion ominaisuus määritelty siinä *itsessään*, tulee käyttää erityistä `Object.prototype`-oliosta periytyvää `hasOwnProperty`-metodia. Tällä tavoin vältämme [prototyyppiketjun](#object.prototype) sisältämät ominaisuudet.
6
4
7
-
> **Note:** It is **not** enough to check whether a property is `undefined`. The
8
-
> property might very well exist, but its value just happens to be set to
9
-
> `undefined`.
5
+
> **Huomio:****Ei** riitä tarkistaa vain että ominaisuuden arvo on `undefined`. Ominaisuus voi hyvinkin olla olemassa. Sen arvoksi on vain satuttu asettamaan `undefined`.
10
6
11
-
`hasOwnProperty` is the only thing in JavaScript which deals with properties and
12
-
does **not** traverse the prototype chain.
7
+
`hasOwnProperty` on ainut JavaScriptin sisältämä metodi, joka käsittelee ominaisuuksia **eikä** käy prototyyppiketjun sisältöä läpi.
13
8
14
-
// Poisoning Object.prototype
9
+
// Object.prototypen myrkyttäminen
15
10
Object.prototype.bar = 1;
16
11
var foo = {goo: undefined};
17
12
@@ -21,33 +16,26 @@ does **not** traverse the prototype chain.
21
16
foo.hasOwnProperty('bar'); // false
22
17
foo.hasOwnProperty('goo'); // true
23
18
24
-
Only `hasOwnProperty` will give the correct and expected result, this is
25
-
essential when iterating over the properties of any object. There is **no** other
26
-
way to exclude properties that are not defined on the object *itself*, but
27
-
somewhere on its prototype chain.
19
+
Ainoastaan `hasOwnProperty` palauttaa oikean ja odotetun tuloksen. Sen tietäminen on olennaista minkä tahansa olion ominaisuuksia iteroidessa. Tämä on **ainut** tapa löytää olion itsensä ominaisuudet prototyyppiketjusta riippumatta.
28
20
29
-
### `hasOwnProperty`as a Property
21
+
### `hasOwnProperty`ominaisuutena
30
22
31
-
JavaScript does **not** protect the property name `hasOwnProperty`; thus, if the
32
-
possibility exists that an object might have a property with this name, it is
33
-
necessary to use an *external*`hasOwnProperty` in order to get correct results.
23
+
JavaScript **ei** suojele `hasOwnProperty`-metodin nimeä. Täten on mahdollista, että olio voi sisältää samannimisen ominaisuuden. Jotta voimme saada oikeita tuloksia, tulee sen sijaan käyttää *ulkoista*`hasOwnProperty`-metodia.
foo.hasOwnProperty('bar'); // palauttaa aina false
43
33
44
-
// Use another Object's hasOwnProperty and call it with 'this' set to foo
34
+
// Käytä toisen olion hasOwnProperty-metodia ja kutsu sitä asettamalla
35
+
// 'this' foohon
45
36
({}).hasOwnProperty.call(foo, 'bar'); // true
46
37
47
-
### In Conclusion
38
+
### Yhteenveto
48
39
49
-
When checking for the existence of a property on a object, `hasOwnProperty` is
50
-
the **only** method of doing so. It is also recommended to make `hasOwnProperty`
51
-
part of **every**[`for in` loop](#object.forinloop), this will avoid errors from
52
-
extended native [prototypes](#object.prototype).
40
+
Mikäli pitää selvittää kuuluuko ominaisuus olioon vai ei, **ainoastaan**`hasOwnProperty` voi kertoa sen. Tämän lisäksi on suositeltavaa käyttää `hasOwnProperty`-metodia osana **jokaista**[`for in`-luuppia](#object.forinloop). Tällä tavoin voidaan välttää natiivien [prototyyppien](#object.prototype) laajentamiseen liittyviä ongelmia.
0 commit comments