FAQ Database Discussion Community


The correct way to implement prototypal inheritance

javascript,oop,inheritance,prototype
There is already numerous threads about prototypal inheritance in JavaScript, but this is not a copy due to laziness. I have literally read them all and have found almost as many different syntactic approaches as I have found answers, so it appears that I am not the only one that...

What's the difference between these 2 implementations of prototypal inheritance?

javascript,inheritance,prototype,prototypal-inheritance
What's the difference between these 2 implementations of prototypal inheritance, and considering that we're working with 2 different "prototypes" (the prototype property that's only on functions, and the internal prototype), and how do these implementations differ in their prototype chain lookup? Also, does the first implementation (using the prototype property)...

javascript constructor.prototype inheriting another constructor.prototype [duplicate]

javascript,oop,inheritance,prototype
This question already has an answer here: Why wouldn't I use Child.prototype = Parent.Prototype rather than Child.prototype = new Parent(); for Javascript inheritance? 3 answers Here is my problem: Let`s say I have a constructor A() with prototype X; And now I have another constructor B which I want...

How to use prototypes such as modules in Node.js?

javascript,node.js,express,prototype
I create a folder called helpers, and inside that, create the array.js file Inside my array.js, i have this code: Array.prototype.inArray() { for(var j in this) { if (this[j] === arguments[0]) { return true; } } return false; } And i want be able to use like a module: myArray.inArray(myargument);...

Unwanted Javascript effect: Prototype is sharing closure between instances

javascript,closures,prototype
I'm making a game with timed levels. A level is 2 minutes long, and a countdown timer is displayed in the upper right corner. Mario style. One way to keep track of the elapsed time would be to have my Level object have elapsed as a member variable. Level.init =...

JS Power Constructor and instanceof without new

javascript,constructor,prototype,new-operator
To quote this guy we can use a power constructor which has the form function constructor() { var that = {}; that.data = true; that.method = function () {}; return that; } To create objects that will behave correctly with or without the new keyword. My question is how do...

Can I save object prototype in local storage?

javascript,prototype
I have JS object with custom methods as prototype (e. g. Foo.prototype.myAwesomeMethod). If I load my object in a localStorage and get it later I'll loose these methods. var foo = new Foo() console.log(foo.myAwesomeMethod()) // WIN localStorage.setItem('foo', foo) foo = localStorage.getItem('foo') console.log(foo.myAwesomeMethod()) // FAIL... Is it possible to save the...

JavaScript oop: Designing classes correctly

javascript,oop,closures,prototype
Taking this list of similar questions: How to set up JavaScript namespace and classes properly Javascript namespace declaration with function-prototype Best OOP approach to these two small JavaScript classes I'd concluded there are two possible ways for implementing classes and instances in JS: using an inner function or using a...

Javascript method chaining definition

javascript,prototype
What is the difference between when using method chaining in Javascript I know that this can have some strange quirks so I am unsure: var Kitten = function() { this.name = 'Garfield'; this.color = 'brown'; this.gender = 'male'; }; Kitten.prototype.setName = function(name) { this.name = name; return this; }; Kitten.prototype.setColor...

Prototype difference between object.create and new Object

javascript,object,prototype
//The last link of the prototype chain always be Object.prototype. var o = {"name":"kannanrbk", "age": 21}; //extending object. var no = Object.create(o); //Now the new object is linked to the old object o. no.name = "Bharathi Kannan R"; console.log(o.name); //Still be 'kannanrbk'. Because, this prototype chain will be used only...

Javascript object as parent for another js object

javascript,oop,prototype
Could you explain me how to do in JavaScript such thing : Create one object. Create second object. Set first object as a parent to second object. Add second object as child to first object. It sounded simple to me as Java developer but I am confused right now. Demo:...

How to push objects into array inside of a JavaScript Prototype?

javascript,angularjs,prototype,angularjs-service,angularjs-factory
I have an Angular Factory whose job is to save special objects and retrieve them later. (Used to save the users workflow when he switches views). Basically I need to save objects with a name, as well as an array of tags. My problem is with the saving the tags...

Why prototype is not available in simple JavaScript object [duplicate]

javascript,prototype
This question already has an answer here: Why is JavaScript prototype property undefined on new objects? 4 answers I am trying to understand the JavaScript prototype and I am bit confused.There are tons of tutorials out there and each has different explanation on it. So I don't know where...

I cannot keep the bean stateful when the spring bean is value = “prototype”, proxyMode = ScopedProxyMode.TARGET_CLASS

spring,proxy,prototype,javabeans
here is the spring bean: @Repository @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS) public class TestBean { private String text; public String getText() { return text; } public void setText(String text) { this.text = text; } } and here is the very simple codes to access the bean: TestBean testBean =...

Understanding an unexpected result due to an unmatched prototype (C89)

c,prototype,c89
I have a program goo.c void foo(double); #include <stdio.h> void foo(int x){ printf ("in foo.c:: x= %d\n",x); } which is called by foo.c int main(){ double x=3.0; foo(x); } I compile and run gcc foo.c goo.c ./a.out Guess what? I get "x= 1" as result. Then I find the signature...

C++ constructor prototype parameter

c++,class,constructor,arguments,prototype
For example, there is a class foo : class foo { public: foo (int = 10); ..... } The prototype of the constructor has "int = 10" inside. So, what does it mean? Int is just an integer type, isn't? So, isn't that illegal to assign a value to it?...

Javascript difference between MyClass.prototype = {} and MyClass.prototype.method [duplicate]

javascript,prototype
This question already has an answer here: Defining a Javascript prototype 5 answers This is one thing I was wondering for a long time. But first example code for both Type A var JavaScriptClass = function () { }; JavaScriptClass.prototype = { myMethodOne: function() { }, myMethodTwo: function() {...

Static methods in JavaScript

javascript,static,prototype
If I have a class declared in prototype.js var ClassFoo = Class.create(); ClassFoo.prototype = { initialize: function() { }, type: 'ClassFoo' }; If I declare a method ClassFoo.doBar = function() { log("foobar") } Is it the same as/equivalent to creating a static method in java ? Can an object of...

Creating functions in function prototypes for Javascript

javascript,function,prototype
How do I properly create a function within a function prototype? What I have is this: <body> <p id="demo"></p><script> function person(first, last, age, eye) { this.firstName = first; this.lastName = last; this.age = age; this.eyeColor = eye; } person.prototype.name = function() { return { myFunc: function() { this.firstName + "...

Constructor function - in simple namespace - Javascript

javascript,object,namespaces,prototype
In want to define a function-constructor inside a namespace. The way in which I defined the constructor till now was a simple constructor function without NS, combined with prototypal inheritance. The code looked kind of like: function mySuperObject() { var self = this; self.p1 = ""; self.p2 = "jkejie"; //...

Binding Object Prototype Functions to Object in Javascript

javascript,prototype,call,this,bind
Background I have a prototype object that has dozens of functions that access info from a list like this: var prototype_object = { yellow: function(list) { return list[this.type+'_yellow']; }, green: function(list) { return list[this.type+'_green']; }, // ... 50 more functions here } The 'this.type' is supposed to come from the...

From prototype to real APP

ios,user-interface,prototype,converter
I am a starter now developing my own IOS APP, but ran into the trouble: I really want to work out some cool UI, and do find lots of tools that will do exactly what I am thinking, however they seems to only give a prototype design. I am wondering...

HTML