Z
Z
ZaxapKramer2016-07-30 07:05:28
JavaScript
ZaxapKramer, 2016-07-30 07:05:28

OOP in Javascript, inheritance: how to implement this?

How to implement similar in javascript if possible?

var put = function(elem, text) {
  var text = encodeURIComponent(text);
  return {
    now: function(a) {
      var a = encodeURIComponent(a) || '';
      add: function() {
        elem.outerHTML += text+a;
      }
      replace: function() {
        elem.outerHTML = text+a;
      }
    }
    after: function(time) {
      // do smth
    }
  }
}

Next, we call it something like this:
put(document.getElementById("div_id"), "Hello ").now("world!").replace;

The problem is that it throws an error on the line with replace (in this case, it's 9): SyntaxError: function statement requires a name .
I understand that something is wrong, but what? Tell. Or is it generally impossible to implement so many "nesting levels"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-07-30
@ZaxapKramer

You have almost everything right, only a couple of syntax errors and one logical one:

var put = function(elem, text) {
  var text = encodeURIComponent(text);
  return {
    now: function(a) {
      var a = encodeURIComponent(a) || '';

      return {
        add: function() {
          elem.outerHTML += text + a;
        },
        replace: function() {
          elem.outerHTML = text + a;
        },
      }
    },
    after: function(time) {
      // do smth
    },
  }
}

put(document.getElementById("div_id"), "Hello ").now("world!").replace();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question