how use object oriented in javascript?

Posted by Sagarp under JavaScript on 2/25/2010 | Views : 1325 | Status : [Member] | Replies : 14
hi

i will try use opp concept in javascript reply me how can i do?

Thanks
SagarP
http://www.emanonsolutions.net
http://emanonsolutions.blogspot.com/



Responses

Posted by: Abhi2434 on: 2/25/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
See javascript is the most flexible language I have ever seen.
You can define methods and directly add them to an object. Like this :

var obj = {

x:function(){
},
y :function(){
}
}


Now you will find obj.x() and obj.y().

Another approach might be
var obj = new Object();

obj["x"] = function(){
};
obj["y"] = function(){
};


Both are similar. But obviously you can define your own class using Javascript just like Date, Array etc. To define a class you need a constructor. Here comes the use of apply() and call() to the Function prototype handy.

Use Like this :

MyClass = function(){

this._init.apply(this, arguments);
};
MyClass.prototype = {
x: null, //a variable
_init: function() {
//this is ur constructor
},
callMe : function(){
}
};

Now from code you can do

var x = new MyClass();

x.callMe();


I hope this is what you wanted here.

Cheers.

www.abhisheksur.com

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/25/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
No one of your links defined about prototype of a function.

So they are not actually a class.


www.abhisheksur.com

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 2/25/2010 [Member] Bronze

Up
0
Down
Prototype-based programming is a style of object-oriented programming in which classes are not present

Check
http://en.wikipedia.org/wiki/Prototype-based_programming

Many people are thinking like only class & object are called as of OOPs. Class or encapsulation is a part of OOPs but not the entire OOPs.

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/25/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
I think you are not totally clear of what Class means.

The main intention of creating a class is just to make objects of it and work readily. Class is just a prototype of an object.

In javascript, I know you can just declare an object just by passing the function to it. It is just because of its flexibilitty that I can assign functions and methods to any variable. Javascript is not truely object oriented. There are lots of things missing with Javascript.

But I know what is the exact meaning of Prototype, and if I go by its meaning, the only thing that defines a type for you is using Prototype approach.

I strongly deny that creating objects like

var x = { a:20, b:30} or 

var x = new object();
x["a"] = 20;
x["y"] = 30;


Is anywhere near declaring a class. Only prototype does. If you define a method to the prototype of a variable, it means you can create new object from it.

like
var x = new MyClass();
just declared.

And it is totally different from
var MyClass = function() {

return { a:20, b:30};
}
var x = MyClass();


So if you go to the depth of Javascript ( I am using it for at least 4 years) you can easily derive, that prototyping is the only way you can make objects appear somewhat similar to what we mean by objects.
Thus if I write

DateTime.prototype.myCustomMethod = function(){
};

I am actually inheriting the type DateTime.

So I recommend you to try out all other methods and derive the logic yourself.

I hope you got my point. Still not.... Give valid reason and example , why you think so. Dont give unnecessary links... all those links are created by peoples like us(other than docs of course).



www.abhisheksur.com

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 2/26/2010 [Member] Bronze

Up
0
Down
I think you are still in a misconception.

Class-Based languages and Prototype-Based Languages are different. Check a url explains the same.

http://www.webreference.com/js/tips/010215.html



Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/26/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
You didnt got the point I am talking about.
I said, the implementation of class, if there is any, should be the one that changes the prototype of an object.

By no other means you can create a prototype of an object.
Did you read the last paragraph :

"JavaScript, being a prototype-based language, does not distinguish between classes and instances. It has only objects, which are similar to instances. An object is a real, particular entity. The shape on the bottom right corner of a drawing is an object. You can use an existing object as a template for creating another object. The second object shares the first object's properties and methods. Any object might be used as a prototype for another object. Any object can also specify its own properties and methods, adding or overriding its existing ones."

It clearly states that prototyping is the only way where you can create objects of similar instance.


www.abhisheksur.com

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: SurajRane on: 3/15/2010 [Member] Starter

Up
0
Down
Refer following article ..

http://www.dotnetfunda.com/articles/article680-classes-and-inheritance-in-javascript-.aspx

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 4/2/2010 [Member] Bronze

Up
0
Down
abhi, You said that you have worked with javascript for more than 4 years(even no body asked, no one has interested to know that too).

Finally you came to a contradictory sentence(to whatever you said initially). You are higlighting whatever that I have given, So be clear about the concept. no matter about the experiences that you gathered and no one requires that. And there is no concept of class in javascript. (initially you said I have not highlighted that one). And the objects that you are talking about is not the exactly same as the objects of OOPs.

If you are not trusting or referring to any links, why you are posting them as answers for the forums.

I don't trust even the links created by people like you.

Know everything clearly before arguing


Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 4/3/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Did you see the link provided by SurajRane ?

www.abhisheksur.com

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 4/7/2010 [Member] Bronze

Up
0
Down
Your words.... Dont give unnecessary links... all those links are created by peoples like us(other than docs of course).


You forgot one thing. Docs also created by people like us.... :)

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 4/7/2010 [Member] Bronze

Up
0
Down
I just would like to repeat the same words.

Class-Based languages and Prototype-Based Languages are different. And don't think class or encapsulation is the only concept in OOPs.

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 4/7/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
I never disagreed how Class Based Language differs with Prototype based language.

I am putting forth my points on regards to Javascript.

:D :D


www.abhisheksur.com

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 4/7/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Javascript requires prototype to define types. Thats what my final word is.

Does it makes sense... :)

www.abhisheksur.com

Sagarp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response