Solved

javascript prototype

Posted on 2015-01-09
2
70 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:9apit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 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:9apit
ID: 40540764
Well done Rainer.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

707 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