I have three simple Javascript object definitions in my HTML page. The first one works but the others seems to cause an error on the page. I am interested to learn to see which way is the most common way to declare an object in Javascript and which way is the best way to declare an object in Javascript. I have used C++, Java, and the .NET frame work and there seems to be a standard way of declaring objects in these types of OOP languages.
I would like to find a way that is standard to declare the objects so that my code is standard across all of the web pages that I work on. That is I would like to have my methods and properties all declared in the object at declaration type and not so much as just add them in as one goes which I think is possible for the most part from what I have seen in the code.
I also declared an empty object which I think is correct.
var Ball = {}; //Shouldn't this be an empty object
Question: I think that I am not completely certain what object means in this sense and how it should be used in Javascript objects?
For example: In Java or C++ the new operator creates a new instance of the object. Like below.
class PPP {
int temp;
}
PPP testobject = new PPP();
The above is how I would create an instance of class PPP in Java but how would this be done in Javascript seems different because I think that the Object keyword is throwing me off here and there is some reason I read (not sure where) that Javascript is somewhat different in that everything is a function or something?????
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>Test</p>
<script>
var myCar = new Object();
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 1969;
var myPickup = new Object();
myPickup.make = "Ford";
myPickup.model = "Mustang";
myPickup.year = 1969;
var Ball = {}; //Shouldn't this be an empty object
var testObj = new Object();
testObj.tst = "test";
var AA = new testObj();
var BB = new testObj();
</script>
</body>
</html>
Error:
The below two object declarations are causing me an error which I am not completely certain as to why? I created the object just like the myCar and created one property and then created tow new objects and I receive an error that is listed below.
var AA = new testObj();
var BB = new testObj();
Uncaught TypeError: object is not a function
More Code:
The following is object code as well but it has a function objectName() as the declaration which i think is extremely confusing for me. Could this be built with
Car c = new Object();
c.desc = somevalue;
c.color = another value;
c.getInfo = function getInfo() {
someCodeGoesHere;
}
But some where I saw that to declare a function outside of the class you have to do it like this. So I think that the prototype work is used for most things.
Car.prototype.getInfo = function() {
return 'A ' + this.color + ' ' + this.desc + '.';
};
How it was defined in the example I saw.
function Car (desc) {
this.desc = desc;
this.color = "red";
this.getInfo = function getInfo()
{
return 'A ' + this.color + ' ' + this.desc + '.';
};
}
var car = new Car('Porsche boxter');
car.color = "blue";
alert(car.getInfo());