![]() |
A little confused about Objects
So I'm reading a book on Javascript trying to get a better understanding of it all. But I've become stuck on Objects. Seeing how js is an Object Oriented language, I'm stopping on this chapter until I fully understand it. There's not one particular part that I find confusing, but it's more that I'm not understanding how I would use something like the Constructor Pattern or getOwnPropertyDescriptor().
I've looked around the internet to find a better description, but no luck so far. Is there a good tutorial somewhere that you guys would recommend? Or could someone at least give an example of how I would use said objects? The book (so far) has been really good. But I find the examples in this section to be vague. (It's using getting a person's name, age, job). In real life javascript, what would I use it for. I think seeing a good example would help me a lot. Thanks |
Constructor Pattern? or do you mean how objects are created through Constructors?
getOwnPropertyDescriptor() is somewhat easier. in ECMAScript 262 edition 5, there were property descriptors introduced, which allow a certain amount of property configuration (e.g. property is not writeable). see also https://developer.mozilla.org/en-US/...defineProperty. getOwnPropertyDescriptor() just gives you access to those descriptors. |
It might be helpful to understand JSON-style object definitions first.
For example: Code:
var john = {Once you have done that, you can then use code such as Code:
someHTMLelement.innerHTML = john.email;Code:
if ( john.title == "Chief Gofer" ) { ... do something ... }So we introduce constructors: Code:
function Person( nm, em, ttl )Code:
var john = new Person( "John Doe", "john@xyz.com", "Chief Gofer" );So a constructor might be thought of as being a "factory" to create many objects of the same kind, each with different content. Constructor notation is about as close as JavaScript comes to matching syntax with other languages (esp. Java and C++) that are class-based and use constructors to create instances of classes. JavaScript doesn't have classes in the same way such more conventional languages do, but it does have objects that, in most ways, act the same way objects in those languages do. [The big difference: JavaScript objects are mutable, whereas C++ and Java objects are not. That is, you can add properties and methods to any JavaScript object at any time. In Java and C++ you can only do so by modifying the class--and recompiling your entire program or set of programs. For example, in JavaScirpt I can use that constructor above and then add to the object. Such as Code:
var john = new Person( "John Doe", "john@xyz.com", "Chief Gofer" ); |
Quote:
you can freeze an object, or use Object.definePoperty() to create non-writable and non-delete-able properties. for example, an employee can change their name, but not a company-assigned ID#: Code:
var john = { |
… or non-enumerable or even properties that are defined by getters and setters (instead by simple values).
|
There are lots of different ways to create objects in JavaScript.
Another useful one that hasn't been mentioned yet in this thread is to use the static method Object.create() |
All the above true. I wasn't trying to be comprehensive.
One thing none of us mentioned but that tends to confuse the heck out of people new to this stuff: Variables are *NOT* and *CAN NOT* be objects! Variables *CAN* be (and commonly are) *REFERENCES* to objects. I was careful in my prior post when I wrote Quote:
Code:
var john = new Person("john doe","john@xyz.com","Chief Gofer");Code:
var object = new Object( );It's a very important distinction! If I now write Code:
var jane = john;Because all that var jane = john did was copy the *reference* to the object. So we ended up with two references to the same object.So don't mistake references for actual objects. We treat them much the same, but they aren't the same. ******************** FWIW, this same distinction applies in Java and C# and, for the most part, in C++. (There are ways for a variable to actually *BE* an object in C++. Not commonly used, but...) |
Quote:
Code:
deepCopy = function(p,c) { |
Quote:
for example: Code:
var john = new RegExp("hello world")Code:
jane=JSON.parse(JSON.stringify( john ));if you want to take your core to the max, a few minor adjustments are needed to make it more like the structured clone algorithm than JSON.stringify(): Code:
function deepCopy(orig, update) {Code:
var john = new RegExp("hello world") |
| All times are GMT +1. The time now is 10:32 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.