10

Ok am just going through basics of JavaScript and I was learning objects where I came across this example...

JavaScript

var person = {
   firstname : "Smith",
   lastname  : "Bach"
};

And what we write in PHP is

$person = array(
    "firstname"=>"Smith", 
    "lastname"=>"Bach"
);

So is this the same thing or am making a mistake in understanding the concept?

1
  • 2
    FYI, PHP's array (mixture of arrays and hashmaps in one data structure) is bad and messy concept. You should rather ask "how that works" instead of "how is this similar to xyz in PHP" ;) Commented Dec 25, 2012 at 14:28

2 Answers 2

11

No, objects are more than that.

Object is indeed a map/dictionary, but additionally every object inherits some of the properties (key-value pairs) from another object. That other object is called prototype.

For example:

var o = {
    x: 1
};

console.log(o.x === undefined);           // false, obviously
console.log(o.toString === undefined);    // false, inherited from prototype

Most commonly a prototype is set by creating an object with a constructor function:

var d = new Date();
console.log(d.hasOwnProperty('getYear'));     // false, it's inherited

EDIT:

Here's how the prototype works using constructor functions (it's one of the ways to do OOP in JS):

// constructor function
// starts with capital letter, should be called with new
var Person = function (name, age) {
    // set properties of an instance
    this.name = name;
    this.age = age;
};

// functions to be inherited are in the prototype
Person.prototype.sayHello = function () {
    return this.name + ' is ' + this.age + ' old';
};

// new:
// - creates the object
// - sets up inheritance from prototype
// - sets the object as the context of the constructor function call (this)
var p = new Person('Jason', 27);

console.log(p.sayHello());
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't want to write a whole book here about prototypes. I'm in a process of making a JS course for people who already know a thing or two about programming, so if you're interested, let me know.
@Mr.Alien Then I would recommend the "JavaScript Patterns" book. It's a good read. If you need more, then try JavaScript: The Definitive Guide".
2

They are associative arrays, but not just associative arrays. There are functions available from the Object prototype (like .toString()) whose names can collide with property names. Objects can be constructed via other functions and given more inherited properties too. (Note that one thing that plain objects don't have is a .length property to count entries, like array objects have. The term "associative array" is probably not the best one to use for JavaScript objects; they're objects and that should be enough once you're familiar with JavaScript.)

edit — what I mean is this:

var o = {};
alert("toString" in o); // alerts "true"

Thus a newly-created empty object appears to have a property called "toString". The issue with JavaScript is that there's only one property accessor operator (well two, but they're two flavors of the same thing), so there's no way to distinguish between accesses to the array's contents and access to the array's API. (Also, in JavaScript it's really not a good idea to think of them using the word "array", as that means something different in JavaScript — arrays are a type of Object with special properties.)

EcmaScript 5 has mechanisms for defining object properties in such a way as to make them immutable and non-iterable, which helps some. It's still problematic if you want to store a property called "toString" in an object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.