/    Sign up×
Community /Pin to ProfileBookmark

Reassigning myobject.prototype to change classes?

Hey everyone, do you think this is okay javascript? Anyone ever done something like this before?

[code]
function Person() { }
Person.prototype.sayHi = function() { alert(“Hello!”); }

var myperson = {id: 3004, name: “Goobles”};
myperson.prototype = Person.prototype;
myperson.sayHi();
[/code]

I’ve never changed the prototype manually. I’m experimenting with it because JSON just returns everything as plain Objects. I need the JSON to return Persons so I can call sayHi on them, but the only way I can think of doing that is to change the prototype.

Tried googling, but all that came up was the Prototype framework. Wish they named their framework differently.

PS. Sorry for the double-post, I was trying to edit the title and something weird happened.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Verdagon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...