answersLogoWhite

0

There are two simple ways to create a new Object in Javascript.

1. Create a direct instance of an object. In this method, we call the native Javascript Object constructor.

myObject = new Object();

myObject.property1 = 'First prop value';

myObject.property2 = 'A different value';

myObject.color = 'blue';

You can get this exact same result with less code by using object literal notation.

myObject = {property1: 'First prop value', property2: 'A different value', color: 'blue'};

Adding a method is also easy. If you have a predefined function, you simply pass the name.

myObject.niftyMethod = ourPreDefMethod;

You can also use anonymous functions here...

myObject.anotherNiftyMethod = function() { alert 'Super cool!'; }

This works fine, but you might find a need for an object constructor, so you can produce objects at will as any given point. In that case, you simply define a function to create an object.

function myObject( property1, property2)

{

this.property1 = property1;

this.property2 = property2;

this.color = null;

//Set a method for changing color

this.changeColor = function(color) {

this.color = color;

}

}

You can also define a function an attach it, same as the other notation.

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What are properties in Javascript?

Like other object-oriented languages (JavaScript is technically not 'object-oriented'), JavaScript has objects, those objects have attributes or 'properties' which hold data


Can Javascript create 3D?

Javascript is a browser-run script, so I highly doubt that Javascript possesses the capabilities to create 3-D images, but it can input them into the webpage.


Is JavaScript or Dreamweaver better?

Not comparable. JavaScript is a programming language. Dreamweaver is an application (which you can use to create pages with javascript, html, etc.)


How do you create a simple JavaScript message inside the HTML?

You need to type the following code to create a simple JavaScript message inside the HTML


How does one define a Javascript class?

To define the class of a javascript code or string there are two to three or so main methods, one of these is to use it, and review the result, such as activating and using a function relative to to itself. Another is to use the objects Literals, the advantage to this is that Literals are a shorter way to define objects and arrays in JavaScript.


Can you create a forum in a webpage?

Yes, we can create a forum on a page. It would require HTML, CSS and JavaScript.


Can an entire website be written in Javascript?

No. The website is still going to need to be displayed in HTML, and CSS is going to be needed to create the layout. While you could use JavaScript to create the HTML structure, you still will need a base HTML document just to get the browser to call the JavaScript's initial functions.


How do you save javascript on Roblox?

It is not possible to save and execute Javascript code on Roblox. You can save Javascript code as plain text in script objects, but there is no way to execute the code. Roblox uses a scripting language called LUA in-game which is somewhat similar to JavaScript, but it is not identicle. You can learn about LUA syntax on the Roblox wiki.


What is a example of a class is Javascript. Describe the properties and one method. Give two examples of objects of that class?

JavaScript is a prototype based object oriented language. This means that it doesn't have classes.


What you need to study to create your website?

I would suggest you start with HTML and then move on to JavaScript VBScript would come after that. EX: <html><body><!--HTML HERE--> <!--JAVASCRIPT STARTS BELOW--> <script language="JavaScript"> //JAVASCRIPT HERE </script> <!--JAVASCRIPT ENDS HERE--> <!--VBSCRIPT STARTS BELOW--> <script language="VBScript"> 'VBSCRIPT HERE </script> </body></html>


Difference between vbscript and java script?

Visual Basic script and JavaScript are both programming languages. JavaScript is used primarily to create websites, and has no user interface. Visual Basic is used to create software applications, has a user interface, and is less heavy on the code than JavaScript.


How do you create a function in HTML?

You don't. You can only create it in Scripting languages like Javascript or Server Side languages like PHP.