Tải bản đầy đủ - 0 (trang)
Objects: Checking for properties and methods

Objects: Checking for properties and methods

Tải bản đầy đủ - 0trang

Line 1 declares an empty array, listOfProperties. Lines 2-4 cycle through all the

properties of plan1, adding each property (push), including any methods, to the array

listOfProperties. Using the example we've been working with, the array

listOfProperties

winds

up

with

a

value

of

"name,price,space,transfer,pages,discountMonths,calcAnnual".

Note that, unlike a regular for loop, this one doesn't establish a limit on the number of

loops or increment a counter. After each iteration, JavaScript automatically moves to the next

property of the object and stops iterating when there are no more properties to enumerate.

Note also that instead of prop, you could use any other legal variable name.

In the example, the method, calcAnnual, wasn't included in the original definition of the

object plan1. It was later added to the prototype of the constructor function that I used to

create the object, and so became a property of the object by inheritance. The code above

includes inherited properties (including methods) in the collection. If you want to limit the list

of properties to those that have been explicitly declared for the object, omitting those that are

inherited from a prototype, you need to use hasOwnProperty.

1 var listOfProperties = [];

2 for (var prop in plan1) {

3

if (plan1.hasOwnProperty(prop)) {

4

listOfProperties.push(prop);

5

}

6 }



In the example above, each property in turn is assigned to the variable prop. Then the if

statement tests to see if it's a property owned by the object as opposed to being inherited

through a prototype.

You can of course test a literal property name instead of using a variable.

var isOwnedProperty = plan1.hasOwnProperty("price");



244



Find the interactive coding exercises for this chapter at:

http://www.ASmarterWayToLearn.com/js/75.html



245



76

Browser control:

Getting and setting the URL

In addition to making things happen on the webpage, you can use JavaScript to control the

browser. To begin with, you can get the browser to tell you its current location.

var whereWeAt = window.location.href;



For

example,

if

the

browser

is

currently

at

http://www.mybeautifulsite.com/products/page33.html#humidifier, the statement above will

assign the string "http://www.mybeautifulsite.com/products/page33.html#humidifier" to the

variable whereWeAt.

You can also get pieces of this. This statement gets just the domain name.

var theDomain = window.location.hostname;



In the example, the string "www.mybeautifulwebsite.com" is assigned to the variable

theDomain. "http://", the path, and the anchor are omitted.

Here's how you get the path.

var thePath = window.location.pathname;



In the example, the string "/products/page33.html" is assigned to the variable thePath. If

the browser were on the home page and the URL were simply

http://www.mybeautifulsite.com, the string "/" would be assigned to the variable.

In the example, the browser has been pointed to a section of the page marked by the

anchor #humidifier. This statement identifies the anchor.

var theAnchor = window.location.hash;



The string "#humidifier" is assigned to the variable theAnchor. If there is no anchor in

the URL, the variable is assigned an empty string, "".

As usual, you can reverse the order of things, telling the browser where to go instead of

asking where it is.

window.location.href = "http://www.me.com/1.html";



The statement above tells the browser to navigate to the page named 1.html at the site

me.com.

If you wanted the browser to navigate to the site's home page, you'd write...

246



window.location.href = "http://www.me.com";



And if you wanted it to land on an anchor...

window.location.href = "http://www.me.com/1.html#section9";



It would be nice if you could use window.pathname = ... to move to a different page

on the current site or window.hash = ... to move to an anchor on the current page, but you

can't. What you can do, though, is query the browser for the domain name and combine that

with the page where you want to go.

1 var currentSite = window.location.hostname;

2 var destination = "http://" + currentSite + "/wow.html";

3 window.location.href = destination;



Here's the line-by-line breakdown:

1. Gets the domain name and assigns it to the variable currentSite. Example:

www.me.com

2. Concatenates the string "http://" with the domain name plus the destination page and

assigns the combo to the variable destination

3. Directs the browser to the destination

This is how to direct the browser to an anchor on the current page.

1

2

3

4



var currentSite = window.location.hostname;

var currentPath = window.location.pathname;

var destination = "http://" + currentSite + currentPath + "#humidifier";

window.location.href = destination;



Here's the breakdown:

1. Gets the domain name and assigns it to the variable currentSite. Example:

www.me.com

2. Gets the pathname and assigns it to the variable currentPath. Example: /1.html

3. Concatenates the string "http://" with the domain name, the destination page, and the

desired anchor and assigns the combo to the variable destination

4. Directs the browser to the destination



247



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Objects: Checking for properties and methods

Tải bản đầy đủ ngay(0 tr)

×