473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

setting an object's prototype directly?

Hi all,

I was hoping I could do something like this in javascript:

var a = {x:1, y:2};
var b = {x:2, z:3};
b.prototype = a;

And then:

b.x => 2
b.y => 2
b.z => 3

The purpose is to set up "b" as an override of "a".

I know it doesn't work that way because "prototype" is not a magic
property on any object but constructors only. Is there a way to
implement the above without involving a constructor?

Thanks!

Jun

Oct 14 '05 #1
3 1154
jy******@gmail.com wrote:
I was hoping I could do something like this in javascript:

var a = {x:1, y:2};
var b = {x:2, z:3};
b.prototype = a;

And then:

b.x => 2
b.y => 2
b.z => 3
The purpose is to set up "b" as an override of "a".

I know it doesn't work that way because "prototype" is not a magic
property on any object but constructors only. Is there a way to
implement the above without involving a constructor?


Ok , I see what your after, but I'm not sure which answer to give you.
perhaps you could explain more about what your doing.

var a = {x:1, y:2 };
var b = {x:2, y:a.y ,z:3 };

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Oct 14 '05 #2
The purpose is to "mark up" / "annotate" / override an object. If I
have a = {x:1, y:2} but want to pass on x:2, I want to create b = {x:2}
with its prototype pointing to a and pass it on so that client gets x:2
from b while getting the rest from a.

Jun

Oct 17 '05 #3
jy******@gmail.com wrote:
Hi all,

I was hoping I could do something like this in javascript:

var a = {x:1, y:2};
var b = {x:2, z:3};
b.prototype = a;

And then:

b.x => 2
b.y => 2
b.z => 3

The purpose is to set up "b" as an override of "a".

I know it doesn't work that way because "prototype" is not a magic
property on any object but constructors only. Is there a way to
implement the above without involving a constructor?

b = new ( ( a = function ( ) { } ).prototype = a );

a.y = 2;
b.x = 1;

alert( [ a.x, b.y ] );

// </joke>
but no, in all seriousness... I'm not aware of a way to do this without
a constructor. The above does work, but it still involves a
constructor.

Wish I had a real answer for you.

--
-r
(also, sorry if the joke was inappropriate)

Oct 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
2
by: Max | last post by:
I just wonder if there is a way of converting an object to a string. I am using a function to send an object parameter e.g "fctng(train0);" later on I would like to use train0 but as a string to...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
6
by: Jake Barnes | last post by:
I was just reading this article on Ajaxian: http://ajaxian.com/archives/show-love-to-the-object-literal This is a newbie question, but what is the object literal? I thought it was like an...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
3
by: Patient Guy | last post by:
Subject line would seem to say it all: How does one trigger the execution of a method within an object or any other code/function with the setting of an object property? More elaboration for...
6
by: Murray Hopkins | last post by:
Hi. THE QUESTION: How do I get a reference to my Object when processing an event handler bound to an html element ? CONTEXT: Sorry if it is a bit long. I am developing a JS calendar tool....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.