Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

JavaScript By Example

106 6
JavaScript is a prototyping language and so we can create objects in JavaScript by making copies of other objects. There are a number of built in objects in JavaScript that you can use within your script directly. You can also extend these objects as well as define your own objects.

All objects in JavaScript can have properties and methods. Properties are very similar to the variables you already know (in fact they are the same thing since all variables that are not defined as properties of an object are properties of the Window object.

Methods are exactly the same as functions with any functions you define separately being methods of the window object.

You reference the methods and properties of an object by specifying the name of the object followed by a dot followed by the method or property.

In this example we will create a new object by copying the Date object and then access the getFullYear method to retrieve the cirrent year for display.

HTML


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Example 22</title>
</head>
<body>
<p id="ex"></p>
<script type="text/javascript" src="example22.js"></script>
</body>
</html>

JavaScript


var today;
today = new Date();
document.getElementById('ex').innerHTML = today.getFullYear();

Source...

Leave A Reply

Your email address will not be published.