FAQ Database Discussion Community


assigning object to prototype

javascript,node.js,prototype
I am trying to assign a object to a prototype, but I keep getting a error that the object is undefined. I am trying to do this //x.prototype.y = {}; StreetEasy.prototype.neighborhoodPaths = {}; and at a later point in the code I am trying to access this object in another...

Syntax Error in creating javascript constructor function

javascript,prototype,function-constructor
I am trying to create a constructer function as follows in javascript. But i get the error: SyntaxError: Unexpected token ILLEGAL I am getting this when i run the following in chrome developer tools console: function People () { ​this.superstar = "Michael Jackson"; } Is there any syntax error in...

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...

How to change toString() for primitive types' implicit conversions in Javascript?

javascript,prototype
As an experiment, I did something like this: Object.prototype.toString=function(){ alert('works!'); } 'test'+{}; // alert is shown However, this doesn't work for primitives: Number.prototype.toString=function(){ alert('works!'); } 'test'+123; // no alert 'test'+(new Number(123)); // no alert (123).toString(); // yes alert Is it possible for primitive implicit conversions to use a custom toString...

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...

How can I create a callable prototype method within another prototype

javascript,prototype
I'm working on trying to build a small library of functions within an object prototype. I'd perviously just been throwing my helper functions as global functions, however i'm trying to migrate to something more self contained. Anyway my proto wrapper looks like this; Q.fn = jHelper.prototype = { // helper...

how do i create an Object.prototype clone

javascript,object,prototype,prototype-chain,prototype-oriented
I have an incomplete clone of Object.prototype, made to the best of my knowledge, can you show me how to finish it!? The Code function duplicate_object_prototype(){ var aRef=[ '__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf', '__proto__' ]; var clone = Object.create(null); for(var i in aRef){ var...

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 prototyping Cannot set property 'moveRight' of undefined

javascript,class,properties,prototype,undefined
I've a problem with this simple prototyping: Game = function (moduleConfig, gameConfig) { this.moduleConfig = moduleConfig; this.gameConfig = gameConfig; // Game-Commands this.keyCommands = { moveLeft: false, moveRight: false }; this.catcher = null; this.stage = null; return this; } /** * Left arrow down */ Game.prototype.onKeyboardLeftDown = function () { this.keyCommands.moveLeft...

Need to add a custom type and use a prototype property within my form

javascript,html,methods,prototype,custom-type
I am trying to create a custom type and then add a method to the custom type using its prototype property within the dummy form I have created. I am trying to wrap my mind around how I could create a custom type that would essentially create a "new subscriber."...

Difficulty Manually Walking The Prototype Chain

javascript,constructor,prototype
I wanted to try manually walking the prototype chain of a few objects just to see what I find along the way. However, I got stuck on the first one that I tried. Here's the code: function MyObject() { } var x = new MyObject(); console.log('--------------------------------------------'); console.log('x.constructor.name: ' + x.constructor.name);...

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...

Is it possible to using Javascript prototypes without using the “new” keyword?

javascript,oop,prototype
The fact that Javascript uses functions to create objects was confusing to me at first. An example like this is often used to highlight how prototypes work in Javascript: function Car(){ this.setModel=function(model){ this.model=model; } this.getModel=function(){ return this.model; } } function Bus(){} Bus.prototype=new Car(); var obj=new Bus(); obj.setModel('A Bus'); alert(obj.getModel('A Bus');...

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);...

How to change an attribute of a javascript prototype object

javascript,object,attributes,prototype
html base <html> <head> </head> <body> <input type="text" class="abc"/> </body> </html> So I have my prototype object function AnArray(){ this.anArray=[]; } AnArray.prototype.getAnArray=function(val){ return this.anArray[val]; } AnArray.prototype.setData=function(index,val){ this.anArray[index].data=val; } var objAnArray=new AnArray(); the object ends up looking like this id: 1, pid: "questions-worth-asking", num: 1, data: null and I'm trying to...

Javascript Prototype method and un-used variables

javascript,node.js,prototype
I am currently building objects via prototype method. This object requires no user built methods and while a factory approach may be better here, I am more comfortable with prototyping. I am wondering about the var buf = data._raw;. What happens to buf once the object is returned, does it...

someFunction.Prototype.constructor vs someFunction.constructor

javascript,constructor,prototype
I am aware of the fact that functions in JavaScript lead a dual life first of a function (as first class thing to create instances from) and the second one of a normal object. But I am surprised to see the output of the following console. function A() { console.info("A");...

How to create a Javascript Hash Table / Associate Array Prototype

javascript,prototype,associative-array
With a associative array such as: var m = {}; m['a'] = {id:1, foo:'bar'}; m['b'] = {id:2, foo:'bar'}; Is it possible to create a prototype such as: Array.prototype.hello = function() { console.log('hello'); } m.hello(); This fails because m is an object, so I tired: Object.prototype.hello = function() { console.log('hello'); }...

HTML