Skip to content

Commit 2e258af

Browse files
committed
Merge pull request BonsaiDen#159 from brunocoelho/dev
Passing wrong object to hasOwnProperty method.
2 parents e50aad9 + 4a94b22 commit 2e258af

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

doc/en/object/hasownproperty.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ on its [prototype chain](#object.prototype), it is necessary to use the
55
`hasOwnProperty` method which all objects inherit from `Object.prototype`.
66

77
> **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
8+
> property might very well exist, but its value just happens to be set to
99
> `undefined`.
1010
11-
`hasOwnProperty` is the only thing in JavaScript which deals with properties and
11+
`hasOwnProperty` is the only thing in JavaScript which deals with properties and
1212
does **not** traverse the prototype chain.
1313

1414
// Poisoning Object.prototype
15-
Object.prototype.bar = 1;
15+
Object.prototype.bar = 1;
1616
var foo = {goo: undefined};
17-
17+
1818
foo.bar; // 1
1919
'bar' in foo; // true
2020

2121
foo.hasOwnProperty('bar'); // false
2222
foo.hasOwnProperty('goo'); // true
2323

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.
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.
2828

2929
### `hasOwnProperty` as a Property
3030

@@ -45,7 +45,7 @@ necessary to use an *external* `hasOwnProperty` to get correct results.
4545
({}).hasOwnProperty.call(foo, 'bar'); // true
4646

4747
// It's also possible to use the hasOwnProperty property from the Object property for this purpose
48-
Object.prototype.hasOwnProperty.call(obj, 'bar'); // true
48+
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
4949

5050

5151
### In Conclusion

doc/en/object/prototype.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
## The Prototype
22

3-
JavaScript does not feature a classical inheritance model; instead, it uses a
4-
*prototypal* one.
3+
JavaScript does not feature a classical inheritance model; instead, it uses a
4+
*prototypal* one.
55

6-
While this is often considered to be one of JavaScript's weaknesses, the
6+
While this is often considered to be one of JavaScript's weaknesses, the
77
prototypal inheritance model is in fact more powerful than the classic model.
88
It is, for example, fairly trivial to build a classic model on top of a
99
prototypal model, while the other way around is a far more difficult task.
1010

1111
JavaScript is the only widely used language that features prototypal
1212
inheritance, so it can take time to adjust to the differences between the two
13-
models.
13+
models.
1414

1515
The first major difference is that inheritance in JavaScript uses *prototype
1616
chains*.
1717

1818
> **Note:** Simply using `Bar.prototype = Foo.prototype` will result in both objects
19-
> sharing the **same** prototype. Therefore, changes to either object's prototype
20-
> will affect the prototype of the other as well, which in most cases is not the
19+
> sharing the **same** prototype. Therefore, changes to either object's prototype
20+
> will affect the prototype of the other as well, which in most cases is not the
2121
> desired effect.
2222
2323
function Foo() {
@@ -36,26 +36,26 @@ chains*.
3636
// Make sure to list Bar as the actual constructor
3737
Bar.prototype.constructor = Bar;
3838

39-
var test = new Bar() // create a new bar instance
39+
var test = new Bar(); // create a new bar instance
4040

4141
// The resulting prototype chain
4242
test [instance of Bar]
43-
Bar.prototype [instance of Foo]
43+
Bar.prototype [instance of Foo]
4444
{ foo: 'Hello World' }
4545
Foo.prototype
4646
{ method: ... }
4747
Object.prototype
4848
{ toString: ... /* etc. */ }
4949

5050
In the code above, the object `test` will inherit from both `Bar.prototype` and
51-
`Foo.prototype`; hence, it will have access to the function `method` that was
51+
`Foo.prototype`; hence, it will have access to the function `method` that was
5252
defined on `Foo`. It will also have access to the property `value` of the
5353
**one** `Foo` instance that is its prototype. It is important to note that `new
54-
Bar()` does **not** create a new `Foo` instance, but reuses the one assigned to
54+
Bar()` does **not** create a new `Foo` instance, but reuses the one assigned to
5555
its prototype; thus, all `Bar` instances will share the **same** `value` property.
5656

57-
> **Note:** Do **not** use `Bar.prototype = Foo`, since it will not point to
58-
> the prototype of `Foo` but rather to the function object `Foo`. So the
57+
> **Note:** Do **not** use `Bar.prototype = Foo`, since it will not point to
58+
> the prototype of `Foo` but rather to the function object `Foo`. So the
5959
> prototype chain will go over `Function.prototype` and not `Foo.prototype`;
6060
> therefore, `method` will not be on the prototype chain.
6161
@@ -71,7 +71,7 @@ hasn't found the specified property, it will return the value
7171
### The Prototype Property
7272

7373
While the prototype property is used by the language to build the prototype
74-
chains, it is still possible to assign **any** given value to it. However,
74+
chains, it is still possible to assign **any** given value to it. However,
7575
primitives will simply get ignored when assigned as a prototype.
7676

7777
function Foo() {}
@@ -85,22 +85,22 @@ creation of prototype chains.
8585
The lookup time for properties that are high up on the prototype chain can have
8686
a negative impact on performance, and this may be significant in code where
8787
performance is critical. Additionally, trying to access non-existent properties
88-
will always traverse the full prototype chain.
88+
will always traverse the full prototype chain.
8989

90-
Also, when [iterating](#object.forinloop) over the properties of an object
90+
Also, when [iterating](#object.forinloop) over the properties of an object
9191
**every** property that is on the prototype chain will be enumerated.
9292

9393
### Extension of Native Prototypes
9494

9595
One mis-feature that is often used is to extend `Object.prototype` or one of the
9696
other built in prototypes.
9797

98-
This technique is called [monkey patching][1] and breaks *encapsulation*. While
99-
used by popular frameworks such as [Prototype][2], there is still no good
98+
This technique is called [monkey patching][1] and breaks *encapsulation*. While
99+
used by popular frameworks such as [Prototype][2], there is still no good
100100
reason for cluttering built-in types with additional *non-standard* functionality.
101101

102-
The **only** good reason for extending a built-in prototype is to backport
103-
the features of newer JavaScript engines; for example,
102+
The **only** good reason for extending a built-in prototype is to backport
103+
the features of newer JavaScript engines; for example,
104104
[`Array.forEach`][3].
105105

106106
### In Conclusion

0 commit comments

Comments
 (0)