Solved

javascript prototype

Posted on 2015-01-09
2
62 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
2 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
Comment Utility
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
Comment Utility
Well done Rainer.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now