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
Solved

javascript prototype

Posted on 2015-01-09
2
68 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

790 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