Define a function, extend the functions prototype, create two instances, prototype was modified?
Date : March 29 2020, 07:55 AM
this will help It's not the prototype that's being modified, but rather the config object you've put on the prototype. This is correct behavior, objects referenced by the prototype are not copied when you create a new instance. f1.config === f2.config, they point to the same object. The way the prototype chain works for get operations is this: +------+ +------+
| f1 | | f2 |
+------+ +------+
| |
+------+-------+
|
v
+--------------------+ +--------+
| [[Proto]] assigned | | config |
| via `new f` |------>| object |
+--------------------+ +--------+
|
+-------+-------+
| |
V v
+------------+ +------------+
| a property | | b property |
+------------+ +------------+ var pseudoProto = {}; // A stand-in for the prototype...
pseudoProto.config = { // ...with `config` on it
a: 'a',
b: 'b'
};
var f1 = {}; // A blank object...
f1.pseudo = pseudoProto; // ...referencing `pseudoProto`
var f2 = {}; // Another blank object...
f2.pseudo = pseudoProto; // ...also referencing `pseudoProto`
f1.pseudo.config.b = "bb"; // Change the `b` property on `config`
console.log(f2.pseudo.config.b); // Logs "bb", of course
var f = function(b){
this.config = {
a: 'a',
b: b
};
}
var f1 = new f('bb');
console.log(f1.config);
var f2 = new f('bbb');
console.log(f2.config);
// Logs
// { a: 'a', b: 'bb' }
// { a: 'a', b: 'bbb' }
|
Javascript extend object's prototype but keep old prototype
Date : March 29 2020, 07:55 AM
I wish this help you It's as simple as using a for-in loop to copy properties over from your existing extend object into Obj.prototype: for (var key in extend)
// make sure it's actually a direct property of extend
if (extend.hasOwnProperty(key))
// copy over value from extend into prototype
Obj.prototype[key] = extend[key];
|
TypeScript not recognizing prototype extension decorator
Date : March 29 2020, 07:55 AM
it fixes the issue Currently there is no way to do that. There is a proposal to make type system aware of decorators, with some people saying in comments that it's hard to implement. In the same thread you can find suggested workaround for typechecking, which uses interface merging. First, you declare an interface describing all the properties that are added by decorator (it can have the same name with the decorator): interface Foo {
bar: String
}
interface Qux extends Foo {}
@Foo()
class Qux {
constructor () {
console.log(this.bar); // -> 'baz'
}
}
|
confusion over the phrase "extend Object.prototype or one of the other build-in prototype"
Date : March 29 2020, 07:55 AM
should help you out The term "built-in prototype" refers to the prototype objects from which standard objects inherit. This includes the language-specified Boolean.prototype, Number.prototype, String.prototype, Symbol.prototype, Object.prototype, Array.prototype, Function.prototype, Date.prototype, and the prototype objects for the various Errors, typed arrays, data structures ((Weak-) Map, Set) and iterators. It also encompasses other native prototype objects in the environment, for example the DOM (Node.prototype, Element.prototype, Document.prototype, …) and other Web APIs (e.g. XMLHttpRequest.prototype).
|
How can I extend a library's decorator?
Date : March 29 2020, 07:55 AM
|