Can we execute a function within our object itself? See code below:
obj = {
name:function(){alert("maizere");}
};
As obj.name() also means the same i.e getting access to an object and executing the function. Why not execute inside the object itself, something like this:
obj = {
name:function(){
alert("maizere");
},
name();
};
Since obj = new obj() == { obj() } right?
I was unsuccessful when I tried this. Is anything wrong here or am I wrong?
6 Answers 6
The following notation:
var obj = {};
Is called "object literal". It expects a list of key/value pairs, being the properties of the object.
And only that.
Comments
The syntax you were trying doesn't make sense.
If you wish to execute a function straight away, you can do the following. This works by converting your function for use as an expression, rather than as a declaration:
obj={
name: (function(){
alert("maizere");
})()
};
However this is not the same as:
obj={
name: function(){
alert("maizere");
}
};
obj.name();
Because this inside each different function call will be different. With the former this wll point to the global object (normally Window in browsers) and in the latter this will point to obj.
An object literal is not a class, so there is no scoping to the root object, similar with the window object. Besides, object literals are not syntactically structured with the intent to do this. It is a heterogeneous list key-value pairs. Like this:
var o = { a : b };
JavaScript doesn't have classes. However, the with statement (seldom used) allows this behavior (to access a property or method on an object without specifying the objects root).
var obj = { name: function() {} };
with ( obj ) {
name();
}
But this construct should not be used in code as it causes problems. But as an answer to your question, it does the job.
Comments
Yes definitely something is wrong.
JavaScript Objects consist of name:value pairs, so you are not allowed to break that pattern and do other stuff. Please have a look at the details
If you want a constructor in the sense of class-based languages this could be a better approach:
// "class" definition and constructor
var MyObject = function() {
this.name();
};
// method
MyObject.prototype.name = function() {
alert("maizere");
};
// create an object instance of your "class"
var obj = new MyObject();
Note that Javascript has no classes. However, the term sometimes helps to understand the approach of Javascript constructors.
2 Comments
JavaScript has several built-in objects, like String, Date, Array, and more.
An object is just a special kind of data, with properties and methods.
The syntax for accessing the property of an object is:
objectName.propertyName
example:
var message="Hello World!";
var x=message.length;
The value of x, after execution of the code above will be: 12
Note how message is the object.
Here's another example where you instantiate the object using new, and add properties to it.
person=new Object();
person.firstname="John";
person.lastname="Doe";
person.age=50;
person.eyecolor="blue";
Now, here's a class example:
function HelloWorld(hour)
{
// class "constructor" initializes this.hour field
if (hour)
{
// if the hour parameter has a value, store it as a class field
this.hour = hour;
}
else
{
// if the hour parameter doesn't exist, save the current hour
var date = new Date();
this.hour = date.getHours();
}
// display greeting
this.DisplayGreeting = function()
{
if (this.hour >= 22 || this.hour <= 5)
document.write("Goodnight, world!");
else
document.write("Hello, world!");
}
}
In this example, HelloWorld is the class.
key : valuepair only.You cannot put an statement there.