How to set class name for JavaScript object

I have a test class

TestKlass = (function() {

    function TestKlass(value) {
      this.value = value != null ? value : 'hello world';
      console.log(this.value);
    }

    return TestKlass;

})();

x = new TestKlass;
x instanceof TestKlass; (gives true)

I have a new empty object

y = {}
y instanceof Object

Can I find ways to set any properties for y , something like this

y.__proto__ = x.__proto__
y.constructor.prototype = x.constructor.prototype

to get this result

y instanceof TestKlass => true

==================================================== ==

UPD:

So. My main goal is to create a CLONE function. Now my solution works for me. Take a look at this code:

Javascript clown javascript js

Object._clone = function(obj) {
  var clone, property, value;
  if (!obj || typeof obj !== 'object') {
    return obj;
  }
  clone = typeof obj.pop === 'function' ? [] : {};
  clone.__proto__ = obj.__proto__;
  for (property in obj) {
    if (obj.hasOwnProperty(property)) {
      value = obj.property;
      if (value && typeof value === 'object') {
        clone[property] = Object._clone(value);
      } else {
        clone[property] = obj[property];
      }
    }
  }
  return clone;
};

CoffeeScript JS Clone Code

# Object clone
Object._clone = (obj) ->
  return obj if not obj or typeof(obj) isnt 'object'
  clone = if typeof(obj.pop) is 'function' then [] else {}

  # deprecated, but need for instanceof method
  clone.__proto__ = obj.__proto__

  for property of obj
    if obj.hasOwnProperty property
      # clone properties
      value = obj.property
      if value and typeof(value) is 'object'
        clone[property] = Object._clone(value)
      else
        clone[property] = obj[property]

  clone

Now you can try to do it.

A = new TestKlass
B = Object._clone(A)
B instanceof TestKlass => true

It works great with Moz FF 13. But I think this is not a cross browser. and proto is deprecated.

I think there is no universal solution. But perhaps it will be useful for someone.

+5
source share
2

, . , , . , , :

1

y.__proto__ = x.__proto__;

, __proto__ . Rhino.

2

y.constructor.prototype = x.constructor.prototype;

. , y.constructor y.__proto__.constructor, y - y.__proto__, , Object.prototype. , y.constructor Object, prototype Object. . .

3 ( )

, , [[proto]] . - , . __proto__ . :

function setPrototypeOf(object, prototype) {
    var constructor = function () {
        for (var key in object) {
            if (object.hasOwnProperty(key)) {
                (function (key) {
                    Object.defineProperty(this, key, {
                        get: function () {
                            return object[key];
                        },
                        set: function (value) {
                            object[key] = value;
                        },
                        enumerable: true
                    });
                }).call(this, key);
            }
        }
    };

    constructor.prototype = prototype;

    return new constructor;
}

, , :

y = setPrototypeOf(y, TestKlass.prototype);

fiddle.

Edit:

, . , . , , , , . . fiddle:

function Value(value) {
    this.get = function () {
        alert(value);
    };
    this.set = function (newValue) {
        value = newValue;
    };
}

var a = new Value(5);
var b = Object._clone(a);
b.set(10);                // setting b to 10 also sets a to 10
a.get();                  // alerts 10, should alert 5

JavaScript , JavaScript , .

JavaScript, . .

+1

obj instanceof SomeConstructor , SomeConstructor - obj.

, .prototype FirstConstructor SomeConstructor.

SomeConstructor = function() {}
FirstConstructor = function() {}
FirstConstroctor.prototype = new SomeConstructor();

var y = new FirstConstroctor();
y instanceof FirstConstructor; // true
y instanceof SomeConstructor ; // true, it bubbles from FirstConstructor.__proto__ to SomeConstructor
+1

All Articles