Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are various problems in your code. Let me try to explain them.</p> <p>First it is highly recommended to not put opening block braces on a single line in JavaScript. Why you may ask? Well run those two code snippets:</p> <pre><code>// using "braces on same line" style (function () { return { key: 'value' }; })(); // using "braces on line by themself"-style (function () { return { key: 'value' } })(); </code></pre> <p>Both snippets will return different results, allthough the only difference is positioning of braces. The reason for this is semicolon insertion. In JavaScript semicolons are optional. So if the parser finds a newline character and the construct infront of the newline makes sense, it will insert a semicolon. In the second example this is what happens after the return statement. If you place your braces on the same line as the previous statement, you can circumvent such bugs.</p> <p>The next thing you got wrong is that JavaScript has classes. JavaScript is an object oriented language, but unlike most other object oriented languages it does not have classes. In JavaScript objects inherit directly from other objects (their so called prototypes). What you currently arre referring to as a class is in reality a constructor function, which when invoked using the <code>new</code> keyword will create a new object, that will inherit from whatever object is stored in the constructors prototype field.</p> <pre><code>var anObject = { key: 'value' }; function MakeAnObject() { } MakeAnObject.prototype = anObject; var o = new MakeAnObject(); console.log(o.key); // will output 'value' </code></pre> <p>If you <em>set</em> a property, the proerty will alwas be set on the object itself, it will never access the prototype chain, when setting a property.</p> <p>If you <em>read</em> a property from an object, that does not have that property, JavaScript will search the objects prototype chain (that is all the objects that inherit from each other) for that property and returns it if found.</p> <p>If an oject has a property itself, it's prototype chain will not be searched, so you can "override" an objects inherited properties by setting the porperty on the objects self.</p> <p>Look at the following example:</p> <pre><code>function MakeThing() { } MakeThing.prototype = { key: 'value' }; var o1 = new MakeThing(), o2 = new MakeThing(); console.log(o1); // will output 'value' console.log(o2); // will output 'value' o2.key = 'other'; console.log(o1); // will output 'value' console.log(o2); // will output 'other' MakeThing.prototype.key = 'changed'; console.log(o1); // will output 'changed' console.log(o2); // will output 'other' delete o2.key; console.log(o1); // will output 'changed' console.log(o2); // will output 'changed' </code></pre> <p>With all that in mind I will have to tell you: there is no such thing as public and private members on an object in JavaScript. Members will <em>always</em> be public. There are some patterns which try to hide away certain information in an object using closures, but they function very different than private members in a traditional programming language. And worse: those patterns are clunky, produce terrible and very bad performing code. I suggest do not use them if you do not absoltuely require to.</p> <p>So, what does all this mean? Well firstly, if you want to share attributes and methods between multiple objects, they will have to inherit from the same prototype and that prototype must contain those attributes and methods. Secondly if you set something on <code>this</code> it will be set on the current instance, not on the prototype. Thirdly have priavte and public members only by convention. If you absolutely require certain information to be strictly hidden from a certain subsystem, there are patterns for this (Crockford sealer unsealer should yield useable results).</p> <p>All that said here a quick try at fixing your objects:</p> <pre><code>function BaseAAR { this._arr = []; // note &gt;this&lt;. You created a global array in your code. }; BaseAAR.prototype.add = function(arg) { var i, addAt; // always use identity (triple) operators when comparing to null! if (arg === null || (addAt = this.findEnterPos(arg))&lt;0) return false; // since adding and not deleting anything, nothing of value will be returned this._arr.splice(addAt, 0, arg); return true; }; // This finds the entry position for in BaseAAR.prototype.findEnterPos = function() { return (this._arr.length + 1); }; BaseAAR.prototype.arrayGet = function(i) { return ((this._arr !== null &amp;&amp; i &gt;= 0 &amp;&amp; i &lt; this._arr.length) ? this._arr[i] : null); }; function StringIdAAR(id, str) { BaseAAR.call(this); // invoke the constructor of the base object this.m_Id = id; // int this.m_String = str; // string } StringIdAAR.prototype = BaseAAR.prototype; // innherit from StringIdAAR prototype </code></pre> <p>I am not completely sure if this code actually still does what you want it to do, but you should get the point how object oriented patterns in JavaScript should look like. If you want to read more on how to write good JavaScript you should absolutely get the book "JavaScript: The Good Parts" by Douglas Crockford.</p> <p><strong>UPDATE:</strong> I also wrote an <a href="http://www.danielbaulig.de/now-thats-class-its-an-object-prototypal-inheritance-in-javascript/" rel="noreferrer">article on JavaScript object orientation and prototype based inheritance</a>. This might be of interest for anybody passing by here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload