Home arrow JavaScript arrow Page 3 - Creating User-Defined JavaScript Objects, Properties and Methods
JAVASCRIPT

Creating User-Defined JavaScript Objects, Properties and Methods


Did you know that the user could create his own objects, properties and methods? This is the third and last part of a series on adding and deleting JavaScript properties and methods. First, we shall see how to add and delete properties and methods at run time. After that I will show you the privilege that JavaScript gives to the user.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
October 09, 2009
TABLE OF CONTENTS:
  1. · Creating User-Defined JavaScript Objects, Properties and Methods
  2. · The Top Level eval Function
  3. · User Created Objects
  4. · Creating User-Created Objects

print this article
SEARCH DEVARTICLES

Creating User-Defined JavaScript Objects, Properties and Methods - User Created Objects
(Page 3 of 4 )

As you can deduce from the previous page, the user can create objects and add properties and methods to the object. The user has to input the constructor function as a string argument to the eval() function, and that would create the object type. The user can then go on to add properties and methods as we did above. If the user wants to add a method, he has to first use the eval function to create the corresponding function, if the function does not already exist in the code.

Let us now create an object type and object, add a property and a method to the object, and then display the added property and execute the added method, as a user would. We shall use the code segments we have already used before.

Type the following and save as an HTML file.

<html>

<head>

<script type="text/JavaScript">


function userAdd()

{

var evalStr = prompt("Enter the two statements","");

eval(evalStr);


//verify if the property and method were added.

alert(theObject.property1);

theObject.method1();

}


</script>

</head>

<body>

<button type="button" onclick="userAdd()">User Add</button>

</body>

</html>

Open the above HTML file. Looking at the code as it is, when you click the "User Add" button, the statements that you type into the prompt box are enclosed in a string and then assigned to the evalStr variable. The eval() function then evaluates the statements. Next, it is verified whether the property and method were actually added.

We shall put in the statements segment by segment. So until the last segment is input, the verification (last two statements in the userAdd() function) will not give the right result.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials