Working with Templates of an Object using JavaScript

Working with Templates of an Object:

JavaScript language is based on objects. In JavaScript, you can create an object in two ways either by creating a direct instance or by creating an object using a function template. A direct instance of an object is created by using the new keyword.

Syntax for creating a new object:

    Obj=new object();

You can also add properties and methods to an object by using a period (.) followed by or a method name.

Syntax to set properties or call a method:

    Obj.name=”Robert”;
    Obj.rollnumber=32;
    Obj.getValue();

Here name and rollnumber are properties and getValue is a method.

Demo:

  • Create a document called template1.html.

    notepad

  • Write the following code:

    doc

    code
  • Note:

    The src attribute of IMG tag is used to specify the path of image file. If the image file is present in the same folder as the source code then you can just specify the name of the file. If the attribute (Relative path) else you have to specify the complete path of the image file (absolute path).

    Search Pattern:

    If the relative path is given the the images are searched in the source code folder only else it goes through folder to folder as specified in the absolute path to reach the specific file.

  • Execute the script by Opening the file in the Web Browser. If you are using Internet Explore click on “Allow Blocked Content” to allow the script to execute and you are using Mozilla Firefox then click on allow “ActiveX Controls”.

    image
Ebook Download
View all
Learn
View all