Questions tagged [prototype-programming]
Prototype programming is a type of object-oriented programming that eschews the use of classes. It is available in languages such as Self, LispStat, JavaScript, and R.
353
questions
0
votes
1
answer
57
views
On the prototype chain of an object, some objects' .constructor.prototype doesn't point to its [[prototype]], but points to that object itself
The source object that printing the prototype chain can be various:
[1, 2, 3] // array literal
{a: 1} // object literal
new Set() // built-in set
new Promise( () =>{} ) // built-in Promise
...
0
votes
0
answers
25
views
Javascript prototype function like Kotlin extensions [duplicate]
I wonder if Javascript Prototype can extend class like Kotlin Extension does.
Example:
fun MutableList<Int>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to ...
0
votes
0
answers
24
views
Image won't render properly when appending properties from dynamic object from JS to the DOM
I already have some images, with container divs, in my HTML file. I have written my JS so that my new image objects are formatted in exactly the same way as my existing HTML images, including the ...
0
votes
1
answer
41
views
How to change from document to "this" in javascript
I am trying to wrap my head around prototype in javascript as well as getting used to the value of this. Currently I have this set to .cf--modal but when I use this to set a variable it does not seem ...
0
votes
1
answer
113
views
How can I pass a value into a function without using function parameters?
I have a factory function that returns a React Query mutation hook, like this:
function useMutationFactory(mutationFunction: (axios: Axios) => (params: any) => any) {
const baseURL = ...
0
votes
0
answers
81
views
javascript real prototype object oriented programming
I have always been fascinated by the notion of "prototype" in javascript, and I have used that to model business domains. But I have not used javascript for actual programming until quite ...
0
votes
1
answer
39
views
`Object.create(Vec2.prototype)` lacks class properties
I would like to instantiate classes without calling new by using Object.create (which is made for it), but how can I get all properties defined aswell?
class Vec2 {
x = 0;
y = 0;
}
a = new ...
1
vote
0
answers
903
views
How do I edit a td after I have clicked my edit button in JavaScript?
I am working on a oop project that I want to use after for my self and add to my portfolio. I'm also using it as a learning project as I'm new to oop.
In this project I want to be able to edit the ...
1
vote
1
answer
74
views
Is it possible to nest functions in an object in a prototype and access the base object? [duplicate]
Update: I can't answer the question, since it's locked, but I have my own solution to this problem at the bottom, working with Jonas' answer
Suppose I have this addition to a prototype. I'll use ...
0
votes
1
answer
136
views
Creating a derived array type in JavaScript
I'd like to make my own JavaScript queue type based on JS Array. I'd like instances of my queue to be fully functional instances of JS arrays. I'd like for them to have some extra methods, starting ...
2
votes
1
answer
51
views
Sub-class of Number persistently gets converted back to built-in Number Object/Function
In an effort not to pollute or extend the prototype Number object, I am attempting to create a class that extends Number. However, when I do this and then try to add anything to it or even add it to ...
4
votes
1
answer
285
views
Why do square brackets around __proto__ makes it enumerable in an object
When using square brackets around __proto__ in a object, __proto__ is enumerable. When no square brackets are provided, __proto__ is not enumerable.
Example:
obj = {"__proto__": "Hello"}
for (var ...
1
vote
2
answers
435
views
Remove the use of eval function from code
One of my favorite pieces of code to use is:
if (!(eval("this instanceof "+arguments.callee.name)))
return eval("new "+arguments.callee.name+"(...arguments)");
This ...
0
votes
1
answer
67
views
GameProgramming, Prototype pattern: How to translate generic class from C++ to C#?
In this fascinating book called Game Programming Patterns, in the Prototype example, the author shows how to use generic class to spawn a monster in a game.
Disclaimer: The author does state that the ...
0
votes
1
answer
51
views
Where is __proto__ declared in Javascript?
Consider this example:
var a = {}
a.b =5
a.hasOwnProperty("b") // return True
a.hasOwnProperty("__proto__") // returns False
If __proto__ itself isn't declared as object's own ...
7
votes
3
answers
660
views
Why is there a need for prototype objects (in functions)?
I've read a ton of materials about prototypes and understand inheritance in general.
However, this is one thing that is bugging me and I cannot figure it out.
On dmitrysoshnikov.com there is a ...
1
vote
2
answers
60
views
How Object constructor comes in the prototype object of any function?
In the code below
function Hello(){
//
}
console.dir(Hello);
My question is:
From where and how Object comes in the prototype object of Hello ?
What is happening behind the scenes to bring ...
1
vote
2
answers
1k
views
Detecting changes in a JavaScript class property?
Assume you have this class:
class Foo {
constructor() {
this.count = 0
}
increaseCountEventually() {
setTimeout(() => {
this.count += 1
}, Math.floor(Math.random() * 1000))
...
0
votes
1
answer
43
views
Are there languages which support concatenative inheritance from multiple prototypes? [closed]
So that both prototypes and their descendants live their own independent lives by default.
I mean freely taking on new properties while forgetting where they come from (like with human memory -- often ...
0
votes
0
answers
30
views
Is these two code snippets using Object Prototype are exactly same?
function Plant () {
this.country = "Mexico";
this.isOrganic = true; ...
4
votes
1
answer
74
views
Does javascript store datatype information beyond the prototype?
I am studying prototyping in Javascript, and for an experiment was trying to strip some basic objects of their information as much as possible. However, I ran into an issue in the following code ...
0
votes
0
answers
29
views
Why is `this` undefined when implementing the event handling pattern? [duplicate]
I am creating a library and one of the patterns I need to implement is event handling. I am writing in TypeScript, but this issue is basically in Javascript. Consider this code:
class MessageRelayer {
...
0
votes
1
answer
441
views
How to write a generic adapter/-interface class for accessing 3rd party objects?
I would like to have a generic class that accepts an object, which is not under my control (e.g. 3rd party lib), and provides an interface for accessing custom properties defined by callbacks.
The ...
0
votes
1
answer
57
views
Dynamically named function call within jQuery chain [duplicate]
I have a similar question to this Stack post; but, would like to call the function in a jquery chain. Here is my pseudo code:
function togglePanels(f) {
var customchain;
if (f == 'n') {
...
0
votes
1
answer
65
views
Access property in class from prototype override
Not sure if my question is worded properly. But basically I have a class, but I want to write a new method for it. Say my class looks like this:
class MyClass {
constructor () {
this....
1
vote
3
answers
447
views
Javascript prototype and modify original object
How can we update passed object in prototype? I have created similar prototype as Array.reverse, but how I can modify original object?
Array.prototype.myReverse = function() {
let arr = [];
...
0
votes
1
answer
91
views
Javascript: How to call a function from multiple prototypes?
Good day,
I'm coding with phaser 3 and javascript then tried doing classes like so:
class gameScene extends Phaser.Scene {
create()
{
// add inputs
this.cursors = this.setDirectional('W', ...
4
votes
2
answers
311
views
How to make a JavaScript object's prototype permanent?
Can I enforce that the prototype of an object not be changed?
Note! There are some requirements:
The object should behave just like a regular object literal (add/remove/configure/modify properties ...
1
vote
1
answer
61
views
How to clone a constructor function so that it constructs a copy of the original type that behaves just like the original, but has its own prototype?
I am trying to clone a constructor function completely, so that it does exactly the same as the original but its prototype is set to a clone of the original.
I can easily clone the original prototype ...
1
vote
1
answer
815
views
Javascript create own library like jquery
I'm trying to create a library and i want to achieve something like JQuery, when we type $(selector). then the IDE will show a list of jquery method like css or content or etc.
So i created a few ...
3
votes
3
answers
66
views
Use Array.prototype.map in a functional, covariant way
Say I have the following input (to be used with Node, but the problem is more general, and not Node related):
Absolute path to a directory, call it dirPathAbs
An array of basenames (call it ...
-2
votes
1
answer
2k
views
Calculate the volume of cylinder with JavaScript using prototype
I'm a JavaScript noob and beginner so don't get too hard on me.
I need to calculate the volume of a cylinder using a constructor function and prototype.
I've got a form with two inputs which I'll be ...
-1
votes
1
answer
73
views
__proto__ of a function in the DOM in Javascript
Why does Element.__proto__ === Node return true? Every function's __proto__ value is function () {}. How can I make a function that has for a __proto__ value another function?
2
votes
3
answers
258
views
proto inheritance from ES6 class [duplicate]
I have an old codebase full of subclasses of a some external class, using prototypal inheritance. Recently, this external class has been ported to an ES6 class, but also has new features I'd like to ...
1
vote
0
answers
31
views
why JavaScript is meant as a prototype based language [duplicate]
The major advantages with Javascript is said to be that it is a prototype based language.
But what does it mean that Javascript is prototype based, and why is that an advantage?
0
votes
0
answers
330
views
JavaScript prototype password generator
I wrote a simple JS script to generate a random password.
My code that work now but with just small letters
function Password(l) {
this.l = l;
}
Password.prototype.generate = function () {
...
0
votes
1
answer
134
views
Extend proto Array
I have a question with the prototypes.
I'm doing a project and I want to create a small library of functions for Array, I have 3 possibilities.
Extend the Array prototype (But I understand that it is ...
0
votes
0
answers
50
views
Override JavaScript class prototype inside a JavaScript class
I tried to override the Text class prototype function doSomething.
The class Editor initialises its txt with Text inside the _initText function.
The issue is my override code has never been really ...
2
votes
0
answers
373
views
how to override express-session destroy method Nodejs
I am working on a project that need to be notified when session destroy is called Or other alternative is that overriding destroy method of the session and implement my codes there. Anyone knows how ...
0
votes
1
answer
1k
views
'this' is undefined inside a prototype method which use Promise and being called from other prototype method of same object? [duplicate]
I am creating a simple form where we have 2 date input and check validity of date object; I am following prototype pattern and create 2 method .
this was working fine so far but when I use Promise ...
0
votes
1
answer
289
views
How to create a table object prototype using arrays in Javascript?
Suppose this constitute a table:
rows:
[
//TABLE 1
{ //TABLE 1 TITLE HEADER
cells: [
{value: ""},
...
0
votes
1
answer
108
views
Javascript object's prototype property
I have a question about prototype object.
I learned that constructor function has prototype property (not [[prototype]] property) and it indicates prototype object.
ex) If function's name is Person, ...
1
vote
2
answers
3k
views
How to add event listener to element created dynamically within prototype?
HTML
<div id="main"></div>
JS
var TestClass = function() {};
TestClass.prototype = {
addElement: function(parentId) {
var parent = document.querySelector('#' + parentId);
...
1
vote
0
answers
26
views
JavaScript Prototyping: Referencing prototype/object function [duplicate]
I'm trying to bind my prototype functions to event listeners, but somehow this isn't working. I'm suspecting this is caused because the scope of the function call is not correct. I have a few issues ...
0
votes
1
answer
54
views
Promise Practical Self-Implement
I want to pass to the then method the data, but when I do this by giving it this.passedResolve it gets undefined
function Promises(callback){
if(callback){
callback(Promises....
0
votes
1
answer
583
views
Angular2 Router - window.opener: Lost Change Detection at forwar
When I open a popup for OAuth, and returning via
window.opener.closeCallbackFunction();
then my ChangeDetection isn't working anymore on the forwarded page.
function _window(): any {
return ...
3
votes
1
answer
988
views
Clarification on the inability of javascript deleting inherited properties.
guys. I'm studying up on properties for objects and one thing caught my eye on a source of info. There was this one part of the whole document that stated this about JS.
Prototype Properties
...
2
votes
2
answers
862
views
Incrementing object id
var nextid = 0
function Animal(name) {
this.name = name
Object.defineProperty(this, 'nextid',
{value: nextid++, writable: false})
}
var animal1 = new Animal('dog')
var animal2 = new Animal(...
0
votes
1
answer
25
views
Statement in TypeError module is not letting function work without second parameter
I want to add a function to String object which searches all string and returns indexes of the word we want to find. When I don't use startIndex parameter it shouldn't throw a second error, because ...
0
votes
2
answers
69
views
Dynamically assign prototype in constructor not works
What happens when we use "new" operator to create an instance for an Object in javascript? And, during the creation, when the prototype for the constructor is assigned?
I try to dynamically assign a ...