1

I want to create a method which automatically implemented when create an instance of an object, exactly like concept of class constructor.

function myString(string) {
  // Storing the length of the string.
  this.length = 0;
  // A private constructor which automatically implemented
  var __construct = function() {
    this.getLength();
  }();
  // Calculates the length of a string
  this.getLength = function() {
    for (var count in string) {
      this.length++;
    }
  };
}

// Implementation
var newStr = new myString("Hello");
document.write(newStr.length);

I have the following error message when implement the previous code:
TypeError: this.getLength is not a function.


UPDATE:
The problem was in this scope. The following is constructor method after updade:

var __construct = function(that) {
  that.getLength();
}(this);
Lion King
  • 28,712
  • 21
  • 69
  • 128
  • `this` in `__construct` is not what you think it is - once you fix that, you'll also need to move that code below where `this.getLength` is defined – Jaromanda X Dec 09 '16 at 00:35
  • @JaromandaX: Sorry, but I don't understand what do you mean about `this in __construct is not what you think it is`. – Lion King Dec 09 '16 at 00:57
  • Possible duplicate of [Constructors in JavaScript objects](http://stackoverflow.com/questions/1114024/constructors-in-javascript-objects) – Ryan Dec 09 '16 at 01:03
  • do you know what **this** is? – Jaromanda X Dec 09 '16 at 01:12
  • @JaromandaX: Yes, `this` is a reference to the object itself. – Lion King Dec 09 '16 at 01:14
  • I've updated my code which works for what your trying to achieve. – li x Dec 09 '16 at 01:16
  • I don't get it. What's the "private constructor" good for? What do you mean by "automatically implemented"? (Sounds like you're looking for the term "[immediately called](https://stackoverflow.com/questions/8228281/what-is-this-iife-construct-in-javascript)") And why don't you simply drop that "inner" constructor and just put the `this.getLength();` statement that you want executed in your actual constructor, `myString`, that you already have? – Bergi Dec 09 '16 at 01:23
  • 2
    Why don't you just use the constructor? What is the benefit of having another function that is "exactly like concept of class constructor" but isn't actually the constructor? – david Dec 09 '16 at 01:29
  • `Yes, this is a reference to the object itself.` - log `this` inside `__construct` to see that it isn't – Jaromanda X Dec 09 '16 at 01:42
  • @JaromandaX: I thank you, the problem was in `this` scope. – Lion King Dec 09 '16 at 01:55

1 Answers1

1

Bergi's answer in this thread is far more relevant: How to define private constructors in javascript?

Though a bit crude you can create a method called init and then call that method at the bottom of your function so when you instantiate a new object that code shall be run.

  function myString(string) {

  //Initalization function
  this.init = function() {
    this.calcLength();
  }

  // Storing the length of the string.
  this.length = 0;

  this.getLength = function() {
    return this.length;
  }

  // Calculates the length of a string
  this.calcLength = function() {
    for (var count in string) {
      this.length++;
    }
  };

  this.init();
}

// Implementation
var newStr = new myString("Hello");
var element = document.getElementById('example');
element.innerText = newStr.getLength();

Edit: I'm aware there are better ways to achieve this, but this gets the job done.

Edit 2: Fiddle https://jsfiddle.net/ntygbfb6/3/

Community
  • 1
  • 1
li x
  • 3,392
  • 2
  • 26
  • 45
  • Don't use `init` methods. Simply place the respective code in the constructor. – Bergi Dec 09 '16 at 01:24
  • Thank you. Your way is an alternative way but not what I want. – Lion King Dec 09 '16 at 01:25
  • @Bergi I just read your answer in another thread +1 the implementation Is really good, I've learn't something new. Edited your answer into mine. – li x Dec 09 '16 at 01:28