1

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?

3
  • 3
    erm, why would you do that? What goal does it serve? Commented Dec 22, 2012 at 14:45
  • Because an object expects only key : value pair only.You cannot put an statement there. Commented Dec 22, 2012 at 14:46
  • No goal.But just wanted to know because when we create the object using constructor function like var obj=new obj(); is equal to obj={obj();} .So this what made me think about it. Commented Dec 22, 2012 at 14:50

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

@pebbl your 1st e.g was the one i was thinking of ...Thank u very much sir.Also i m clear now
1

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

0

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

1 Comment

No sir .I found the way exactly i was thinking i.e var obj={name:(function(){alert("")})()}.
0

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

what's the difference between class and object.In my view since i m new to programming object is the collection properties and method than what is a class.o thanks for ur post
There are fundamental differences between classes and objects. Easily said, a class is a group of objects with the same properties. In class-based languages an object is an instance of exactely one class. In JS there are no classes but objects can share a prototype objects which adds its properties to the super object. This is called prototypal inheritance. You should consider to learn and understand these basics first.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.