[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Object Oriented JavaScript

zaheer.bcit

9/6/2014 12:00:00 PM

Here is a good read for understanding Object oriented programming in JavaScript.

http://conceptf1.blogspot.com/2014/08/object-oriented-javas...
4 Answers

John Harris

9/6/2014 2:51:00 PM

0

On Sat, 6 Sep 2014 04:59:56 -0700 (PDT), zaheer.bcit@gmail.com wrote:

>Here is a good read for understanding Object oriented programming in JavaScript.
>
>http://conceptf1.blogspot.com/2014/08/object-oriented-javas...

It says :
"In JavaScript class is a function which contains methods and
properties."

Oh dear! Oh dear! Oh dear! Oh dear!

Not recommended.

John

Scott Sauyet

9/8/2014 12:57:00 AM

0

John Harris wrote:
> zaheer.bcit@gmail.com wrote:
>
>> Here is a good read for understanding Object oriented programming in
>> JavaScript.
>>
>> http://conceptf1.blogspot.com/2014/08/object-oriented-javas...

> It says :
> "In JavaScript class is a function which contains methods and
> properties."
>
> Oh dear! Oh dear! Oh dear! Oh dear!

Or going back to the beginning

| JavaScript supports object oriented programming because it supports
| inheritance through prototyping also properties and functions, which
| are building blocks of OOP.

And here I thought OOP had something to do with encapsulation and
dynamic dispatch...

Uggh, for an article so light on material, it's amazing how much this
one gets wrong.


> Not recommended.

To say the least.

-- Scott

zaheerahmed

9/14/2014 1:12:00 PM

0

>Here is a good read for understanding Object oriented programming in JavaScript.
>
>http://conceptf1.blogspot.com/2014/08/object-oriented-javas...

It is nice article for beginners. If someone found any issue in this
article, Please point that out I will surely try to update post.
--


Thomas 'PointedEars' Lahn

9/14/2014 9:26:00 PM

0

zaheerahmed wrote:

>>Here is a good read for understanding Object oriented programming in
>>JavaScript.
>>
>>http://conceptf1.blogspot.com/2014/08/object-oriented-javas...
>
> It is nice article for beginners.

For fitting values of â??niceâ?. As it is, it does a very good job at
misleading beginners by disseminating the many misconceptions of its author.
The Subject of the article and this thread already shows that.

> If someone found any issue in this article, Please point that out I will
> surely try to update post.

OK, then, for startersâ?¦

(The following statements regarding â??JavaScriptâ? apply to all programming
languages on which either ECMAScript is based, or that are implementations
of the ECMAScript Language Specification up to and including Edition 5.1,
excluding the abandoned Edition 4 Working Draft, respectively, whose name
contains â??JavaScriptâ?.)

| JavaScript supports Object Oriented programming, it will not force it
| while compiling by throwing errors.

That does not make sense. JavaScript *is* an object-oriented programming
language, *always*. But that is not obvious, in particular it is not
obvious that globally available symbols are actually names of properties of
the built-in global object. And compilation has nothing to do with any of
this; the latter part of that statement is gibberish at best.

| JavaScript supports object oriented programming because it supports
| inheritance through prototyping

Correct.

| also properties and functions,which are building blocks of OOP.

The building blocks of OOP include, but are not limited to, encapsulation
(properties of objects and _methods_ of objects to operate on them),
inheritance, and polymorphism. So if you replaced â??functionsâ? with methods,
and put a comma before â??alsoâ?, that sentence would have a chance to make
some sense.

| There is no difference in Namespace object and class object used in
| JavaScript unlike C# or other OOP languages.

Wrong. At this time, JavaScript does not support namespaces, which are a
form of lexical scoping. You have to emulate them by using a variable
identifier or property name *like* a namespace. One important difference
between a namespace identifier and a variable identifier or property name is
that you can assign a value to a variable or property (which may or may not
be successful).

| // global namespace
| var OOPJS = OOPJS || {};
|
| Here in above code it is checking if namespace(object) of OOPJS is already
| existed within applications then use the same object but if it is not
| created then it create an empty object and initialize our
| object(namespace).

Wrong (and gibberish). It tests whether there is an identifier â??OOPJSâ? in
the scope chain of the current execution context for a variable or property
whose value can be converted to boolean â??trueâ?. If so, that value is
assigned to the â??OOPJSâ? variable declared previously; if not, a new Object
instance is created and a reference to it is assigned as value of that
variable. Because variable declarations take effect before assignment, a
reference to a new Object instance will only be assigned if the same
variable was not declared *and* initialized with a true-value before.

Although it might look convenient to some, I recommend to avoid that pattern
because if the previous value is a true-value, there will be an unnecessary
assignment. The following is functionally equivalent but more efficient:

if (!OOPJS)
{
var OOPJS = {};
}

(It can also be written on one line.)

| Core Classes/Objects
|
| JavaScript has many classes available for use, [â?¦]

The â??JavaScriptâ? languages in question have no classes as the inheritance is
solely prototype-based (along the ECMAScript Language Specification
construct, the Prototype Chain). They have data types though; one data type
is the Object type, and there are several built-in objects with
constructors: Object, Array, and so on. Those must not be confused with
classes.

> like in C# there is Math class

â??Mathâ? is not a class; it is a built-in object referred by the property name
of the global objects, which, by contrast to several other built-in objects,
is not the identifier of a constructor. This is a result of the requirement
of making Netscape JavaScript similar to Java, which was very popular at the
time. By contrast, C# is a recent addition to the family of OOPLs.

| which contains static function Abs(), round()

As there is no class-based inheritance, the concept of static _methods_
cannot be fully applied to those languages. For example, there is no
implicit way, like with PHPâ??s â??selfâ? keyword, to refer to a class who has
the method as its static method. And it is possible to transfer methods to
other objects, or call them as methods of other objects. â??thisâ? in such a
method always refers to the â??thisâ? value, which depends on how the
method/function is called. Insofar the concept of â??static methodâ? is only
useful in JavaScript as to indicate that the callable property is not
inherited implicitly, in contrast to methods of prototype objects (short:
prototypes).

As those languages are case-sensitive, the method is Math.abs(), not
Math.Abs().

| See here a math class random() function [â?¦]

Gibberish.

| Like Math there is object Object available which contains length(),
| prototype(), create() and many others.

Nonsense. There is no built-in Object.length(), there is no
Object.prototype.length(), and Object instances have no built-in length()
method. â??prototypeâ? is _not_ a method (a callable property); it is a
property of Function instances (Object is one) referring to their associated
prototype. Object.create() is a built-in method in implementations of
ECMAScript Edition 5 and above only.

| Since each JavaScript object is an instance of object Object [â?¦]

Wrong. It is only correct to say that the *specified* *built-in* objects
have the object referred to by â??Object.prototypeâ? in their prototype chain
by default.

| Note all these methods and properties also inherited to namespace too
| since it is also an object.

Ex falso quodlibet.

| So if you want to add a function to all the objects, you can add it to
| Object using prototype().

Nonsense; â??prototypeâ? is _not_ a method. Ignoring that for a moment, you do
not want to augment the object referred to by the â??prototypeâ? property of
the object referred to by â??Objectâ?, unless you are prepared to deal with the
fact that using objects as data containers will become more difficult: those
properties are enumerable by default.

Also, given that in some DOM implementations host objects inherit from
â??Object.prototypeâ?, legacy scope chain issues could cause user-defined
properties on â??Object.prototypeâ? to interfere with script code in markup
documents.

| Custom Classes/Objects

As there are no classes, there are no custom classes.

| JavaScript contains no separate class statements like C#, Java or other
| programming languages.

Nonsense. In neither of those languages is a class a statement; it is a
declaration in each one. JavaScript is similar in that a constructor for a
user-defined object can be, but does not have to be, declared using a
function declaration.

| Here we declare a class and then created two instance of the class.
|
| function Student() { }
| var student1 = new Student();
| var student2 = new Student();

No, here we declare a function with the identifier/name â??Studentâ?, creating
a new Function instance (upon Variable Instantiation/Declaration Binding
Instantiation, when control enters the execution context). That function is
subsequently called as a constructor because of the â??newâ? keyword. When a
function is called as a constructor, its return value is a new object which
has the current value of the Function instanceâ??s â??prototypeâ? property as the
next object in its prototype chain, unless the function returns a reference
to another object. The Specification wording suggests that this new object
can be called a â??Student instanceâ? here. However, it is different from an
instance of a class in that it can be modified.

| In next article we will see classes in details with different access
| modifiers

That concept does not exist in the JavaScript languages. It can only be
emulated to some degree.

| and other aspects of Object Oriented Programming in JavaScript.

One wonders, is the author even aware that he is not talking about a single
programming language, and therefore must choose their wording carefully in
order not to produce factually false statements? Are they aware that they
need to consider the subject matter as referring to several different
implementations of several different Editions of the ECMAScript Language
Specification? Are they aware that the use of those languages is not
limited to Web browsers?

| Please comment your valuable feedback and suggestion for improvement of
| this post.

[x] done

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not Cc: me. / Bitte keine Kopien per E-Mail.