What is ECMAScript 5?
ECMAScript is the official name of what we all call JavaScript. That doesn’t mean we’re wrong; it’s just that the name “JavaScript” is a trademark of Oracle; so Ecma International (originally the European Computer Manufacturers Association—hence ECMA) uses the term “ECMAScript” to refer to the standard of JavaScript. The latest version of this standard is the 5th edition, and it was approved just over a year ago (on December 3, 2009). It encompasses a huge range of great additions, and several of those are starting to show up in browsers. The implementations of ECMAScript 5 is called JavaScript 1.8.5.In this tutorial, we’re going to be looking at the JavaScript 1.8.5 functions that are available to us in the Firefox 4 betas. You’ll be happy to discover that most of the latest versions of other browsers have these, too . . . except for one. This time, it’s Opera, as IE9 has included many of these.
Function 1: Object.create
This method is a very important one; it really cleans up prototypal inheritance. Previously (in ECMAScript 3rd edition), to create an object and set its prototypeFunction 2: Object.defineProperty
If you’ve got an object that you want to define a property on, you’ll probably do it this way:- my_dog.age = 2;
Object.defineProperty
. The first parameter is the object you’re assigning the property on. The second parameter is the name of the property, as a string. The final property is the descriptor object. Here’s how that works. It’s (obviously) an object and it can have a combination of the following properties, all of which describe the property we’re adding:- value: use this to set the value of a property. Defaults to
undefined
. - writable: use this boolean to define whether this is a read-only variable. If it’s writable, it’s
true
. Defaults tofalse
. - configurable: use this boolean to define whether the type (value vs. method) of this property can be changed, or whether the property can be deleted. If it’s configurable, it’s
true
. Defaults tofalse
. - enumerable: use this boolean to define whether this property is included when the properties of the object are enumerated (a for-in loop or the keys method). Defaults to
false
. - get: use this to define a custom getter method. Defaults to
undefined
. - set: use this to define a custom setter method. Defaults to
undefined
.
obj.prop = val
standards. Also, know that you can’t define value
or writable
when get
or set
are defined, and vice versa.Function 3: Object.defineProperties
If you want to define several properties at once, you can use a property descriptors object just as withObject.create
to define them, using Object.defineProperties
.Function 4: Object.getOwnPropertyDescriptor
If you ever want to know the specifics of a property, you can use this function,Object.getOwnPropertyDescriptor
. Take note of the “Own”; this only works with properties on the object itself, not up its prototype chainFunction 5: Object.keys
Ever wanted to get all the keys of an object? Now, you can do so easily withObject.keys
. Pass this function an object, and it will return an array of all the enumerable properties of that object. You can also pass it an array, and you’ll get back an array of the indices.Function 6: Object.getOwnPropertyNames
This one is just likeObject.keys
, except that it includes all the properties—even the ones that aren’t enumerable. By the longer function name, you can tell they discourage the use of it. Usually, you’ll want keys
instead.Function 7: Object.preventExtensions / Object.isExtensible
If you’ve ever wanted to create a function that doesn’t accept new parameters, you can do so now. Run your object throughObject.preventExtensions
, and it will decline all attempts to add new parameters. This function goes hand in hand with Object.isExtensible
, which returns true
if you can extend the object and false
if you can’t.Function 8: Object.seal / Object.isSealed
Sealing an object is one step up from preventing extensions. A sealed object won’t let you add or delete properties, or change properties from a value (like a string) to an accessor (a method) or vice versa. You’ll still be able to read and write properties, of course. You can find out if an object is sealed by usingObject.isSealed
.Function 9: Object.freeze / Object.isFrozen
Freezing it yet another step further. A frozen object can’t be changed in any way; it’s read-only. You can verify the frozenness of an object with, you guessed it,Object.isFrozen
.Function 10: Array.isArray
You’d think that it wouldn’t be too hard to determine that a given variable is an array. After all, everything else works fine with thetypeof
operator. However, JavaScript arrays are of inconsistent ilk. They’re actually closer array-like objects (even though we usually use that term to refer to things like arguments
and NodeList
s). This function gives you a way to be 100% sure that what you’re working with is an array. Pass it a variable, and it returns the boolean.Function 11: Date.prototype.toJSON
This isn’t too big, but if you ever want to store dates in JSON, you might find this useful. Date objects now have atoJSON
function that will convert the date to a JSON string date.Function 12: Function.prototype.bind
You’re probably familiar with usingcall
and apply
to reassign the value of this
in a function.But Wait, There’s More …
Those are the ECMAScript 5th Edition (or JavaScript 1.8.5) functions that have been added to the Firefox 4 betas. There are a few other changes to JavaScript that they are implementing as well, which you can check out in the release notes.However, there are a bunch of ECMAScipt 5 functions that were already supported in Firefox 3, and several other browsers. Have you played with any of these?
- Object.getPrototypeOf
- String.prototype.trim
- Array.prototype.indexOf
- Array.prototype.lastIndexOf
- Array.prototype.every
- Array.prototype.some
- Array.prototype.forEach
- Array.prototype.map
- Array.prototype.filter
- Array.prototype.reduce
- Array.prototype.reduceRight
http://net.tutsplus.com/tutorials/javascript-ajax/whats-new-in-javascript-1-8-5/
No comments:
Post a Comment