Задать вопрос
Ответы пользователя по тегу ООП
  • Наследование в JS. Как вызвать метод родителя по цепочке наследования?

    mlnkv
    @mlnkv
    JavaScript Developer
    Почему не воспользоваться готовой реализацией? Например, Backbone
    function Class() {}
    
    // Helper function to correctly set up the prototype chain for subclasses.
    // Similar to `goog.inherits`, but uses a hash of prototype properties and
    // class properties to be extended.
    Class.extend = function(protoProps, staticProps) {
      var parent = this, child, prop;
    
      // The constructor function for the new subclass is either defined by you
      // (the "constructor" property in your `extend` definition), or defaulted
      // by us to simply call the parent constructor.
      if (protoProps && protoProps.hasOwnProperty('constructor')) {
        child = protoProps.constructor;
      } else {
        child = function() { return parent.apply(this, arguments); };
      }
    
      // Add static properties to the constructor function, if supplied.  
      for (prop in parent) child[prop] = parent[prop];
      for (prop in staticProps) child[prop] = staticProps[prop];
    
      // Set the prototype chain to inherit from `parent`, without calling
      // `parent` constructor function.
      var Surrogate = function() { this.constructor = child; };
      Surrogate.prototype = parent.prototype;
      child.prototype = new Surrogate;
    
      // Add prototype properties (instance properties) to the subclass,
      // if supplied.
      for (prop in protoProps) child.prototype[prop] = protoProps[prop];
    
      // Save parent prototype
      child.prototype.__super__ = parent.prototype;
    
      return child;
    };
    
    var Model = Class.extend({
      constructor: function() {
        console.log("Model:constructor()");
      }
    });
    
    var User = Model.extend({
      constructor: function() {
        this.__super__.constructor();
        console.log("User:constructor()");
      }
    });
    
    var SuperUser = User.extend({
      constructor: function() {
        this.__super__.constructor();
        console.log("SuperUser:constructor()");
      }
    });
    
    var supeUser = new SuperUser();
    Ответ написан
    4 комментария
  • JavaScript Как передать функции один из параметров - объект?

    mlnkv
    @mlnkv
    JavaScript Developer
    function calc(x, y, operation) {
    	var operations = {
    		plus: function(x, y) { return x + y },
    		minus: function(x, y) { return x - y },
    		multiply: function(x, y) { return x * y },
    		divided: function(x, y) { return x / y },
    	}
    	return operations[operation] && operations[operation](x, y);
    }
    
    calc(10, 20, "plus"); // 30
    Ответ написан
    1 комментарий
  • Как разобраться с методом и его свойством?

    mlnkv
    @mlnkv
    JavaScript Developer
    вот описание класса

    свойство устанавливается в методе
    public function setDescription($description) {
      $this->description = $description;
    }


    этот метод вызывается в
    public function setMetaData($name, $content, $http_equiv = false, $sync = true) {
      $name = strtolower($name);
      if ($name == 'generator') {
        $this->setGenerator($content);
      } elseif ($name == 'description') {
        $this->setDescription($content);
      } else {
        if ($http_equiv == true) {
          $this->_metaTags['http-equiv'][$name] = $content;
          // Syncing with HTTP-header
          if($sync && strtolower($name) == 'content-type') {
            $this->setMimeEncoding($content, false);
          }
        } else {
          $this->_metaTags['standard'][$name] = $content;
        }
      }
    }


    осталось найти, где вызывается метод

    ->setMetaData('description', .....
    Ответ написан
    Комментировать