Jump to content
Larry Ullman's Book Forums

Chapter 8: Page 275 Creating A Utility Library


Recommended Posts


var U = {

 $: function(id) {

  'use strict';

    if(typeof id = 'string') {

     return document.getElementById(id);

    } // end of typeof

 } // end of $() function

} // end U object

 

This is a rather simple question. The text says the $() function is a property of U, isn't this function technically a method and not a property?

 

 

Thanks,

Mark

Link to comment
Share on other sites

Yes. There are many different ways to refer to "things" attached to objects. Some people call everything attached to an object a property, and other people differentiate between properties and methods.

 

At the end of the day, the behavior is the same, so it doesn't matter, but I agree that it's more explicit to call $ a method, not a property.

 

"A rose by any other name ...," I guess.

Link to comment
Share on other sites

The function/method is a property of the object because the method is really only an object assigned to a property. When you call the property, the method is invoked. Everything, even functions, are objects in Javascript.

 

For clarity, you should call them methods, though they technically are objects assigned to properties. Because the act like methods, they should be called that though.

Link to comment
Share on other sites

window is the master object of Javascript. Both navigator and document are properties of the window object, even though navigator and document are both objects themselves.

In other words, an object attached to an object is a property of the parent object.

 

Another way to put it: Anything that's not a function attached to an object is a property. Only functions attached to objects can be methods (even though some people also call those properties too).

Link to comment
Share on other sites

Hmmm... I don't think that's right. I only hesitate to say that with 100% certainty because there are some sights out there that claim the navigator object is above the window object, but that makes no sense since window.navigator is used. I'm pretty sure the following is right:

http://www.irt.org/a...s169/object.gif

 

Actually, after testing things, I'm certain that the window object is the top object and the navigator is in the window object. Run the following JS code:

 

for (var prop in window) {

document.body.innerHTML += prop + '<br>';

}

 

You'll see navigator in the list.

Then run this code:

 

for (var prop in navigator) {

document.body.innerHTML += prop + '<br>';

}

 

No window object.

That settles that.

Link to comment
Share on other sites

My apologies for dragging this on with a bland example (I'm still learning this whole deal about objects), but in this simple example dog has 3 properties - color, weight, and name. And then it has one method which is bark.

 



var color = 'brown';
var weight = 50;
var name = 'fido';

var dog = {

color: color,
weight: weight,
name: name,
bark: function() {

var bark = 'woof!';

if(name == 'fido') {

return 'bark';

} else {

return 'meow';
}

}

};

alert(dog.color);
alert(dog.weight);
alert(dog.name);
alert(dog.bark());

 

A comparison would be all the built in methods that javascript already comes with such as toLowerCase(); It's a method.

 



var greeting = 'HELLO WORLD';

alert(greeting.toLowerCase());


Link to comment
Share on other sites

Interesting thread.

 

I agree with Jon. It would be possible to store the window object as a property inside the navigator object (for unknown reasons), but as the navigator object doesn't hold the window object, I'll agree with that the parent object must be the window object. That is also what I've always read.

Link to comment
Share on other sites

Interesting thread.

 

I agree with Jon. It would be possible to store the window object as a property inside the navigator object (for unknown reasons), but as the navigator object doesn't hold the window object, I'll agree with that the parent object must be the window object. That is also what I've always read.

 

I actually found the website where i originally got this info from

 

http://www.comptechdoc.org/independent/web/cgi/javamanual/javanavigator.html

 

If the navigator is a representation of the client internet browser then technically it is higher level than the window object, even though the window object contains the navigator object. How can i window display a browser, the window is displayed in the browser, i guess this is what this guy was implying. Whatever though i don't really care, document and window are enough for me to get my work done and make $$$.

Link to comment
Share on other sites

Hmmm... I don't think that's right. I only hesitate to say that with 100% certainty because there are some sights out there that claim the navigator object is above the window object, but that makes no sense since window.navigator is used. I'm pretty sure the following is right:

http://www.irt.org/a...s169/object.gif

 

Actually, after testing things, I'm certain that the window object is the top object and the navigator is in the window object. Run the following JS code:

 

for (var prop in window) {

document.body.innerHTML += prop + '<br>';

}

 

You'll see navigator in the list.

Then run this code:

 

for (var prop in navigator) {

document.body.innerHTML += prop + '<br>';

}

 

No window object.

That settles that.

 

Its a nice diagram you have there, i checked to see if i can could find a contact for that guy on http://www.comptechdoc.org/independent/web/cgi/javamanual/ but there is no contact email. May be we can find one through his whois, we need to write into him and ask him to update his misleading website. The trouble is if we were to keep pinching every small point we would never get anything done, somethings we need to let them be.

Link to comment
Share on other sites

It's very possible that that information was correct.

JS started back during the days of Netscape Navigator, and the language and specs have changed a lot since then.

The site looks old, so maybe that info used to be right; it makes sense for the top JS object in Netscape Navigator to be the navigator object.

 

Edit: I found a Contact Us link on the page, but it's broken. It's obviously a very old site (relatively speaking), and hasn't been updated for a long time.

Link to comment
Share on other sites

 Share

×
×
  • Create New...