Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Object Oriented JavaScript Date: Sun, 14 Sep 2014 23:25:40 +0200 Organization: PointedEars Software (PES) Lines: 213 Message-ID: <3533429.rOESbTgnJp@PointedEars.de> References: <4a5f1026-4349-471a-8dc2-92d0869d5f1e@googlegroups.com> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1410729970 19080 eJwVx9EVgDAIA8CVCoREx0Es+49Qffd1GTS2wCRyPvFu+b+rORvC7UBVPqvHpekyciW1w5x1AA4bEHk= (14 Sep 2014 21:26:10 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Sun, 14 Sep 2014 21:26:10 +0000 (UTC) User-Agent: KNode/4.14 X-User-ID: eJwFwYEBwDAEBMCVKvxjnCD2H6F3UArbjaBhsYkbFfUe6osJDgmPvd47IXZNewmpl6c/1fKdnPMomjwo/wFm1hWx Cancel-Lock: sha1:f6psFtxE+oDjCDhLDOVyYPscqRc= X-NNTP-Posting-Host: eJwFwYkBwCAIBLCVoNzjOiJ1/xFMWEodQxR4eRU1nT6NVO2Bs+JbTPcfBSo8C/KOaNrjBwAcD9I= Xref: csiph.com comp.lang.javascript:25854 zaheerahmed wrote: >>Here is a good read for understanding Object oriented programming in >>JavaScript. >> >>http://conceptf1.blogspot.com/2014/08/object-oriented-javascript.html > > 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: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not Cc: me. / Bitte keine Kopien per E-Mail.