4

I'm trying not to duplicate questions obviously as I have seen some questions/answers regarding Douglas Crockford's Javascript the Good parts book

I understand most of this code

Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Function.method('inherits', function (Parent) {
this.prototype = new Parent( );
return this;
});
var Mammal = function (name) {
this.name = name;
}.method('get_name', function () {
return this.name;
}).method('says', function() {
return this.saying || '';
});
var Cat = function (name) {
this.name = name;
this.saying = 'meow';
}.inherits(Mammal)
var myCat = new Cat('bagsley');
myCat.get_name();

what I'm having trouble getting is the this.prototype[name] why isn't it written as this.prototype.name; I know returning this allows chaining and the syntax here looks very similar to jQuery but I still don't get the prototype[name] part

Any help is apprecaited thanks

asked May 12, 2012 at 21:50
4
  • stackoverflow.com/questions/4968406/… Commented May 12, 2012 at 22:02
  • It's assigning the parameters to a new object that's why can't use the dot notation. Commented May 12, 2012 at 22:02
  • @elclanrs No, the dot notation cannot be used because the property name is a dynamic string. Commented May 12, 2012 at 22:05
  • Yeah that's what I was thinking just expressed my self wrong. name is a string Commented May 12, 2012 at 22:15

2 Answers 2

3

There is a difference between obj.name and obj[name].

This...

obj.name = 123;

...will assign the value 123 to the "name" property (of the object).

On the other hand, this...

obj[ name ] = 123;

...will assign the value 123 to those property which name is equal to the value of the name variable/argument.

So:

var name = 'foo';
obj.name = 123;
obj[ name ] = 456;
// And now:
obj.name; // 123
obj.foo; // 456
answered May 12, 2012 at 22:02
Sign up to request clarification or add additional context in comments.

5 Comments

i plugged this into firebug and it worked as to prove your point but obviously I would never run this code var name = 'frank' Function.prototype.method = function (name, func) { this.prototype.frank = func; return this; }; Function.method('frank', function (Parent) { this.prototype = new Parent( ); return this; }); var Mammal = function (name) { this.name = name; }.method('frank', function () { return this.name; }) var Cat = function (name) { this.name = name; this.saying = 'meow'; }.frank(Mammal) var myCat = new Cat('bagsley'); myCat.frank();
@user974959 Hm, I didn't quite get your last comment. Do you have another question?
no question just putting your answer to practice meaning this would work ... var name = 'frank' Function.prototype.method = function (name, func) { this.prototype.frank = func; return this; };
@user974959 I don't understand that code. Why did you hard-code this.prototype.frank? The method method will not work properly if it's defined like that. It has to be this.prototype[name], so that the argument value is used for the name...
it could if every method's name was 'frank' however it's not intended to work that way obviously was just illustrating the difference and why it's preferable to use brackets instead of .notation - just putting what I learned to practice basically
2

prototype[name] allows name to be a variable containing any string, and it will access the property of prototype named by the string. prototype.name would look for the property literally called "name".

The [] syntax allows you to:

  1. Have variable property names - especially useful when looping through properties with for..in
  2. Use symbols in property names that are otherwise disallowed (eg. obj['foo-bar'], arr[123])
  3. Have closer resemblance to the associative arrays of other languages such as PHP.
answered May 12, 2012 at 22:01

1 Comment

thanks for your answer Kolink it was very helpful I appreciate your feedback

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.