?
Solved

javascript prototype

Posted on 2015-01-09
2
Medium Priority
?
72 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 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:9apit
ID: 40540764
Well done Rainer.
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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

771 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