LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-03-2011, 02:15 PM   #1
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
Javascript: Adding method to Array type


I heard it was possible in Javascript to add a method to object types. So I tried adding a method to the Array type, like so:

Code:
Array.prototype.contains = function(elem) {
    for(i in this) {
    	if(elem == this[i]) { return true; }
    }
    return false;
};
...for seeing if an element exists in this array. But whenever I insert this into my code, even without actually using it, it somehow messes up the parsing of the script and causes another function in another part of the script not to be recognized. Do I have the syntax wrong, here?
 
Old 10-04-2011, 09:01 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

The syntax is ok. I can't think of any reason this will mess up parsing. Are you sure it's inserted in "the right place"?

There could be a problem if some other code also adds a function with the same name. For example, if the array is two dimensional, your function will fail (return false). Maybe some other code expects it to also check multidimensional arrays?

I would try to use a different function name that's not so generic. Does it solve the problem?
 
1 members found this post helpful.
Old 10-04-2011, 12:43 PM   #3
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467

Original Poster
Blog Entries: 60

Rep: Reputation: 51
Quote:
Originally Posted by Guttorm View Post
Hi

The syntax is ok. I can't think of any reason this will mess up parsing. Are you sure it's inserted in "the right place"?

There could be a problem if some other code also adds a function with the same name. For example, if the array is two dimensional, your function will fail (return false). Maybe some other code expects it to also check multidimensional arrays?

I would try to use a different function name that's not so generic. Does it solve the problem?
Thanks for the response. To be honest, I had given up on getting an answer and converted my code back to the simpler form, so it is difficult for me to check now. However, I was going to try writing some unit tests later and will try a different name then.

I don't know if this insight helps, but it happens that the other code the failed was actually an array manipulation -- where I pulled an object element from an array and then called a method of that object. The error claimed that the method did not exist. So I am thinking somehow my code damages the Array type.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [Perl] OOP Accessor Method for Array Attributes and Append Method brianmcgee Programming 1 10-18-2010 11:05 AM
JavaScript: How to have a method call itself. RHLinuxGUY Programming 1 05-22-2007 12:19 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM
How can I tell one object to do a method in javascript? eantoranz Programming 1 10-29-2004 01:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration