prototypal-inheritance

plain objects VS class instances for model objects

冷暖自知 提交于 2021-02-08 15:20:14
问题 What is the best practice for creating model objects in Angular / TypeScript: Should I use type annotation with object notation (objects are plain instances of Object )? E.g. let m: MyModel = { name: 'foo' } Should I use the new operator (objects are instances of the respective prototype)? Should these two approaches be mixed up and used situationally? (E.g plain objects, when receiving a response from the HttpClient , but new MyModel('foobar') for convenience to create instances by passing

How to override a javascript IIFE returned method in an object?

徘徊边缘 提交于 2021-02-08 12:01:19
问题 I have one IIFE content which looks like this: var A = (function() { var method1 = function() { alert("PARENT METHOD"); } var method2 = function() { method1(); } return { method1: method1, method2: method2 } })(); I want to override this method1 in another javascript object in a way when this method2 executes then it will call the overriden method1, not this original method1. Thanks in advance. 回答1: You are going to need to use prototype in order to accomplish this the way you would like.

Concatenative inheritance vs class inheritance in JavaScript

≡放荡痞女 提交于 2020-12-25 05:00:13
问题 concatenative inheritance works like a composition to me when I look to it at the beginning, but people keep naming it as an inheritance. classes, however, use the prototype to create a prototype chain that connects objects together. the question now is, if both concatenative inheritance and class inheritance do the same thing which one to use? here is an example of both scenarios concatenative inheritance function Person(name, address) { const _name = name const _address = address const

Extending an Array properly, keeping the instance of subclass

不想你离开。 提交于 2020-01-17 00:27:09
问题 I've written a class trying to extend the native Javascript Array class with a custom class, let's call it MyClass . This is basically what it looks like: class MyClass extends Array constructor: (obj) -> @push.apply @, obj first: -> @slice 0, 1 Instantiating the class is no problem. Running this in the console: var myInstance = new MyClass(["1", "2"]) > ["1", "2"] myInstance instanceof MyClass > true myInstance instanceof Array > true works as exptected. The problem is that if I run:

How could this be true??? obj2.__proto__.isPrototypeOf(obj2) //true

微笑、不失礼 提交于 2020-01-16 09:22:14
问题 Consider this short code: let obj1 = { name: "obj1", } const obj2 = Object.create(obj1); obj2.name = "obj2" If you console.log(obj2), it will show this in Google Chrome (Version 79.0.3945.88 (Official Build) (64-bit)): {name: "obj2"} name: "obj2" __proto__: name: "obj1" __proto__: constructor: ƒ Object() Or, you better check this console screenshot image: From what Google Chrome presents, it is obvious that first proto of obj2 is obj1. It is logical too. How come then, that this is true: obj2

JavaScript prototype inheritance and html canvas

Deadly 提交于 2020-01-15 02:37:36
问题 I'm a Ruby developer who finally decided to learn JavaScript seriously. So I purchased some books and I started to dive in, but I got stuck quickly when I tried to understand prototypal inheritance... One of the examples of the book is the following. Given a Shape which prototype has a draw method, and two child shapes: a Triangle and a Rectangle which prototype inherit from Shape; when I call the draw function on Triangle and Rectangle instances the method will draw them properly. when I add

JavaScript prototype inheritance and html canvas

若如初见. 提交于 2020-01-15 02:36:46
问题 I'm a Ruby developer who finally decided to learn JavaScript seriously. So I purchased some books and I started to dive in, but I got stuck quickly when I tried to understand prototypal inheritance... One of the examples of the book is the following. Given a Shape which prototype has a draw method, and two child shapes: a Triangle and a Rectangle which prototype inherit from Shape; when I call the draw function on Triangle and Rectangle instances the method will draw them properly. when I add

What are patterns you could use with prototype inheritance that you cannot with class?

北慕城南 提交于 2020-01-12 02:58:07
问题 Everyone seems to generally agree that prototype inheritance is simpler and more flexible than class inheritance. What I have not seen in the literature that I've read is very many examples of things that you can do with prototype inheritance that you cannot with classical. So I pose a simple question: What are some patterns that you can use with prototype inheritance that you cannot with class inheritance and what is the guidance you would give as far when/if to use it? 回答1: One difference

What is the reason ES6 class constructors can't be called as normal functions?

╄→尐↘猪︶ㄣ 提交于 2020-01-04 07:00:56
问题 ES6 class constructors can't be called as normal functions. According to ES6 a TypeError should be raised when this is done. I used to think that classes were just syntactic sugar for a constructor function + functions in the prototype, but this makes it slightly not so. I'm wondering, what was the rationale behind this? Unless I missed something, it prevents calling the function with a custom this , which could be desirable for some patterns. 回答1: A revisit to the ES6 spec shows how calling

Prototypal inheritance in PHP (like in JavaScript)

时光总嘲笑我的痴心妄想 提交于 2020-01-03 08:50:13
问题 Is it possible to employ some kind of prototypal inheritance in PHP like it is implemented in JavaScript ? This question came to my mind just out of curiosity, not that I have to implement such thing and go against classical inheritance. It just feels like a interesting area to explore. Are there prebuild functions to combine classical inheritance model in PHP with some sort of Prototypal inheritance with a combination of anonymous functions? Let's say I have a simple class for UserModel