JavaScript Good Parts Learning Notes-Object Chapter

Source: Internet
Author: User
Tags hasownproperty

1
Retrieves the value of an object, which can be used to enclose a string expression (a valid identifier and not a reserved word). Point
Recommended points, as this is more compact and readable.
If a property that does not exist is retrieved, undefined is returned.
2
Attempting to retrieve values from undefined would throw a TypeError exception. This
Can be guarded against with the && operator:
Flight.equipment//undefined
Flight.equipment.model//throw "TypeError"
Flight.equipment && Flight.equipment.model//undefined
3
Objects are passed by reference and they are never copied.
Objects is passed around by reference. They is never copied:


4
The prototype (prototype) is not very understanding, the original English copy down.

Every object is linked to a prototype object from which it can inherit properties. All
Objects created from object literals is linked to Object.prototype, a object that
Comes standard with JavaScript.
When youmake a new object, youcan Select the object, the should is its prototype.
The mechanism that JAVASCRIPT provides to does this are messy and complex, but it can
be significantly simplified. We'll add a Create method to the Object function. The
Create method creates a new object, uses an, object as its prototype. There
'll is much more about functions in the next chapter.
if (typeof object.create!== ' function ') {
Object.create = function (o) {
var F = function () {};
F.prototype = O;
return new F ();
};
}
var another_stooge = object.create (stooge);

The prototype link has a no effect on updating. When we do changes to an object,
The object ' s prototype is not touched:
another_stooge[' first-name '] = ' Harry ';
another_stooge[' middle-name '] = ' Moses ';
Another_stooge.nickname = ' Moe ';
The prototype link is used only in retrieval. If we try to retrieve a property value from
An object, and if the object lacks the property name, then JavaScript attempts to
Retrieve the value from the prototype object. And if that object is lacking the
property, then it goes-its prototype, and so on until the process finally bottoms out
With Object.prototype. If the desired property exists nowhere in the prototype chain,
Then the result is the undefined value. This is called delegation.
The prototype relationship is a dynamic relationship. If we add a new property to a
Prototype, that property would immediately is visible in all of the objects that is
Based on this prototype:
Stooge.profession = ' actor ';
Another_stooge.profession//' actor '
We'll see more about the prototype chain in Chapter 6.

5
Check the properties of the object (excluding functions and properties on the prototype chain)
You can use the hasOwnProperty function
The other approach are to use the hasOwnProperty method, which returns true if the
Object has a particular property. The hasOwnProperty method does
Prototype chain:
Flight.hasownproperty (' number ')//True
Flight.hasownproperty (' constructor ')//False

6
Delete can remove properties of an object
Another_stooge.nickname//' Moe '
Remove nickname from Another_stooge, revealing
The nickname of the prototype.
Delete Another_stooge.nickname;
Another_stooge.nickname//' Curly '

JavaScript Good Parts Learning Notes-Object Chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.