Comments (9)
This is only valid if your writing a library or a framework for external use. In which case you would have a line at the top of your (thousands) of lines of code like
var hasOwnProperty = Object.prototype.hasOwnProperty
For almost all normal use it's safe to assume someone isn't a complete idiot and overwrites methods defined on Object.prototype. Only in cases where your dealing with completely untrusted 3rd party code would you need this.
Like for example libraries like underscore.js and jquery.js need to be this idiot proof.
from javascript-garden.
Raynos: yeah, agreed.
from javascript-garden.
As Raynos already said, this mainly benefits fool-proof libraries. I may add a note though, for those out there who might think that hasOwnProperty
cannot be overwritten. But in the end, there will always be a way in which you can break things in JS.
from javascript-garden.
Hi!
The idea wasn't that much on making a library in the face of a developer who does extremely foolish thing. I was more thinking about what to consider when using a JavaScript object as the equivalent of C++ std::map<string, Object>, Python's dict or Rubys Hash.
Consider we want to have a object to store all our friends by value. They might be called "Ryan", "Brendan", "Douglas" and "hasOwnProperty". In none of the languages quoted above, our friend with the weird name would break anything. In JS, one option is to add a prefix to all names (e.g. "mapped_" or whatever). Another option is to just accept all keys are they are and use the trick in original issue to work it out.
I'm not a JS expert so I don't know if that's even possible, considering other friends might be called "prototype", "proto", "constructor" or "defineGetter". But if that's a viable path, one might want to mention the trick above.
Of course, that might be a whole chapter in a whole different book, when I think about it... :)
Cheers,
Kosta
Edit: escaped markdown for foo
from javascript-garden.
And now you have two friends with the same name... ;)
In the end, you use an array or an object with IDs as keys :P
from javascript-garden.
If you want to garantuee that using keys doesn't break anything you'll need a wrapper for the native object type. The thing is, javascript is dynamic, these things can't be avoided without losing your runtime dynamic nature of the language. In the languages you quoted you actaully just pass a key, value pair in which is stored internally.
from javascript-garden.
I found a better example. Lets say you want to count the words in a text. Here's the naive approach:
function count(text) {
var words = text.match(/[\w+']/g),
counts = {},
i, n = words.length;
for(i = 0; i < n; ++i) {
if (counts.hasOwnProperty(words[i])) {
counts[i]++;
} else {
counts[i] = 1;
}
}
return counts;
}
Now, if "hasOwnProperty" appears in the text, we're screwed. Are there other words that also screw us? Is there another way to fix this except
var prefix = 'word_';
...
counts[prefix+i] = ...
from javascript-garden.
Well in this case {},hasOwnProperty()
would indeed be a good solution. I'll add a note about the possibility of hasOwnProperty
being set and the solution.
@Raynos Just a note on the "idiot" proof thing, jQuery dropped the use of hasOwnProperty
they just don't care anymore.
from javascript-garden.
Pushed: 558d91a
Closing now.
from javascript-garden.
Related Issues (20)
- notes about the typeof operator
- notes about type-casting
- footnote for requestAnimationFrame and setImmediate HOT 2
- Length property of Array is not actually the items count HOT 3
- Пропущена буква (not to write. I had to fork. please remove) HOT 1
- Mistake in the section "The Array Constructor" HOT 1
- Wrong output value written in Why Not to Use eval Section HOT 4
- Topic Name : Hidden Use of eval
- ES translation issue in "Notation of Keys" section. HOT 5
- Incorrect detail in section `#object.prototype` HOT 2
- 使用typeof检测一个未定义的变量时,是不会报错的。 HOT 3
- Issue with function evaluation HOT 1
- Mistake in "scopes and namespaces" HOT 2
- The github page is not work HOT 2
- GitHub Pages Website Copyright Styling Error
- move to MDN HOT 1
- The instanceof Operator
- Live Link Issue HOT 1
- Is project still alive? HOT 3
- 链接打不开了吧??
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript-garden.