[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

javascript prototype

Posted on 2015-01-09
2
Medium Priority
?
80 Views
Last Modified: 2015-01-09
Hello expert,

Working on learning about using inheritance in JavaScript
through the prototype property. Looking at some code at
http://www.javascriptkit.com/javatutors/proto3.shtml

This code is offered at the tutorial
++++++++begin code+++++++++++++++
//First, create the custom object "circle"
function circle(){
}
circle.prototype.pi=3.14159

// create the object method
function alertmessage(){
alert(this.pi)
}
circle.prototype.alertpi=alertmessage

++++++++end code+++++++++++++++

So I put it in an HTML page in <script> tags.
Complete page copied herewith below.
But the page did not do anything. Then I realized
that the function had to be called so I
called the function with the onLoad event
in the body tag.

This ran the function but returned 'undefined'.
When JavaScript returns 'undefined ' that
usually means that the variable had not
declared. So looking at the code the
object 'alertpi' does not seem to be defined
so I tried
'circle.prototype.alert=alertmessage'
But still no luck.

Any idea how to make the script work?

Thanks.

Allen in Dallas






++++++++++++++begin page+++++++++++++++++
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE>JS_prototype</TITLE>

<STYLE>
BODY {font-family: Arial;}
</STYLE>

</HEAD>

<body onLoad=alertmessage()>

<h1>Prototype property</h1>

<script>

//First, create the custom object "circle"
function circle(){
}
circle.prototype.pi=3.14159

// create the object method
function alertmessage(){
alert(this.pi)
}
circle.prototype.alertpi=alertmessage

</script>

</BODY>
</HTML>
++++++++++++++end page++++++++++++++++++++
0
Comment
Question by:Allen Pitts
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 2000 total points
ID: 40540747
Hi,
you will have to create the object and use the alertpi property/function like here
http://jsfiddle.net/66rmhwab/

HTH
Rainer
0
 

Author Closing Comment

by:Allen Pitts
ID: 40540764
Well done Rainer.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question