473,388 Members | 1,574 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,388 software developers and data experts.

Enforcing object interfaces

Any opinions on this code as part of a general purpose framework?

I expect people will have varying opinions on the use of $ in the
property names. I did it for several reasons: 'interface' is
reserved,
I didn't want to step on 'constructor', and a 'prototype' property on
a
non-function might be confusing.

Gadget is a usage example to make it easier to follow what I'm doing.

var Gadget = createConstructor({

/*
* $inherit specifies another constructor to 'inherit' from.
* The specified constructor must be one that was returned
* by a previous call to createConstructor.
*/
$inherit: Widget,

/*
* This is a list of the methods to be exposed publicly
* on the objects.
*/
$interface: ['method1', 'method2'],

$prototype: {
method1: function() { ... },
method2: function() { ... },
method3: function() { ... },
data1: 'blah'
},

$constructor: function() {
/* Usually call Widget constructor first.
* Don't use Widget.call because that applies an interface
* wrapper that we don't use. See createConstructor below.
* The $constructor property is added as an alternative.
*/
Widget.$constructor.call(this, 'arg1', 'arg2');

/* other normal constructor stuff */
}
});

var createConstructor = function() {

/*
* createInterfaceWrapper takes a list of methods names
* and creates a function that can be applied to an
* object to create a wrapper object that only exposes
* the methods in the list. This is really the point
* of the whole exercise.
*/
function createInterfaceWrapper( methods ) {
return function( obj ) {
var wrapper = {};

for (var i=0; i < methods.length; ++i) {
var method = methods[i];

wrapper[method] = function(method) {
return function() {
return obj[method](arguments);
};
}(method);
}

return wrapper;
}
}

/*
* Helper function for creating a new object that
* inherits from some prototype object
*/
function inherit(obj, prototype) {
var F = function() {};
F.prototype = prototype;
var newObj = new F();

for (i in obj) {
//Do I need hasOwnProperty here?
newObj[i] = obj[i];
}

return newObj;
}

return function(params) {
/* I've left out handling of default behavior for properties of
* params that might be missing
*/

params.$interface =
params.$interface.concat(params.$inherit.$interfac e);

var interfaceWrapper = createInterfaceWrapper(params.
$interface);

var ctor = function() {
params.$constructor.apply(this, arguments);
return interfaceWrapper(this);
};

ctor.prototype = inherit(params.$prototype,
params.$inherit.prototype);

/*
* Save $interface and $constructor for use by other
* constructors that want to 'inherit' from this one
*/
ctor.$interface = params.$interface;
ctor.$constructor = params.$constructor;

return ctor;
}
}();
Aug 13 '08 #1
8 1203


carton wrote:
I expect people will have varying opinions on the use of $ in the
property names. I did it for several reasons: 'interface' is
reserved,
The closest thing to a general-purpose set of JavaScript conventions I
know is:

http://javascript.crockford.com/code.html

But like all conventions, it doesn't cover everything. It bans the '$'
in names, but has no provision for name/reserved word conflict
resolution. You might try using the Python convention which is to
append an underscore: interface_.
Aug 13 '08 #2
On Aug 13, 3:25 pm, carton wrote:
Any opinions on this code as part of a general purpose
framework?
Wouldn't it seem more sensible to determine the practicality/value of
a general purpose framework first and then, if not rejected, worry
about what code goes into it?
I expect people will have varying opinions on the use of
$ in the property names.
Are the names machine generated? If not, disregarding the specified
convention for the use of $ symbols in identifiers is not a good idea.
Professional programmers would never knowingly do that, and repeating
the mistakes of armatures, no matter how widespread, is no
justification.
I did it for several reasons: 'interface' is reserved,
I didn't want to step on 'constructor', and a 'prototype'
property on a non-function might be confusing.
How hard were you trying if you did not see obvious alternatives?
Gadget is a usage example to make it easier to follow what I'm doing.

var Gadget = createConstructor({
<snip>
}();
You have not said what this is supposed to be for; what it is intended
to achieve or which problems it is intended to solve.
Aug 13 '08 #3
On Aug 13, 1:09 pm, Henry <rcornf...@raindrop.co.ukwrote:
>
Wouldn't it seem more sensible to determine the practicality/value of
a general purpose framework first and then, if not rejected, worry
about what code goes into it?
If you want to start that discussion then I'm interested in your
opinion.
Are the names machine generated? If not, disregarding the specified
convention for the use of $ symbols in identifiers is not a good idea.
Professional programmers would never knowingly do that, and repeating
the mistakes of armatures, no matter how widespread, is no
justification.
If it is so important then I can change it. No need to be so
aggressive. Opinions such as this are why I'm posting this in the
first place.

>
You have not said what this is supposed to be for; what it is intended
to achieve or which problems it is intended to solve.
It is a helper function for creating a constructor that allows the
specification of a restricted public interface.
Aug 13 '08 #4
carton <ct******@gmail.comwrites:
On Aug 13, 1:09 pm, Henry <rcornf...@raindrop.co.ukwrote:
>Are the names machine generated? If not, disregarding the specified
convention for the use of $ symbols in identifiers is not a good idea.
Professional programmers would never knowingly do that, and repeating
the mistakes of armatures, no matter how widespread, is no
justification.

If it is so important then I can change it. No need to be so
aggressive. Opinions such as this are why I'm posting this in the
first place.
Opinions on $ are strong in part because it isn't just a convention,
it's part of the spec, ecma-262 section 7.6:

"This standard specifies one departure from the grammar given in the
Unicode standard: The dollar sign ($) and the underscore (_) are
permitted anywhere in an identifier. The dollar sign is intended for
use only in mechanically generated code."

>You have not said what this is supposed to be for; what it is intended
to achieve or which problems it is intended to solve.

It is a helper function for creating a constructor that allows the
specification of a restricted public interface.
Personally, I'm wary of attempts at creating enforced private
interfaces, mostly because they seem to serve no purpose, but also
because the only way to do that in currently widespread ES
implementations is to use closures, which seems like overkill.

The situation is more or less the same in Perl, which I happen to know
very well, and I've never seen much use for enforced private
properties/methods there either.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Aug 13 '08 #5
On Aug 13, 2:13 pm, Joost Diepenmaat <jo...@zeekat.nlwrote:
>
Opinions on $ are strong in part because it isn't just a convention,
it's part of the spec, ecma-262 section 7.6:

"This standard specifies one departure from the grammar given in the
Unicode standard: The dollar sign ($) and the underscore (_) are
permitted anywhere in an identifier. The dollar sign is intended for
use only in mechanically generated code."
Thank you, I didn't realize that.
Personally, I'm wary of attempts at creating enforced private
interfaces, mostly because they seem to serve no purpose, but also
because the only way to do that in currently widespread ES
implementations is to use closures, which seems like overkill.

The situation is more or less the same in Perl, which I happen to know
very well, and I've never seen much use for enforced private
properties/methods there either.
This is the classic debate about the need to enforce the boundary
between the interface and the implementation. The only alternative is
to trust users of your objects to 'follow the rules' and not access
properties that you may want to change. For small projects perhaps it
doesn't matter but for larger projects with multiple developers I
consider enforcement a necessity.

However, considering that enforcement has a runtime overhead in
javascript it is probably worthwhile to provide a flag to enable/
disable it so we can turn it off for production releases but still
develop and run unit tests with the enforcement enabled. That should
be easy to add to my implementation.



Aug 13 '08 #6
On Wed, 13 Aug 2008 at 07:25:08, in comp.lang.javascript, carton wrote:
>Any opinions on this code as part of a general purpose framework?

I expect people will have varying opinions on the use of $ in the
property names. I did it for several reasons: 'interface' is
reserved,
I didn't want to step on 'constructor', and a 'prototype' property on
a
non-function might be confusing.

Gadget is a usage example to make it easier to follow what I'm doing.
<snip>

You're using javascript to implement a new OO language. Why?

* Helper function for creating a new object that
* inherits from some prototype object
<snip>

In other OO languages a function that creates new objects is called a
constructor, not a 'helper'.

John
--
John Harris
Aug 13 '08 #7
On Aug 13, 4:53 pm, John G Harris <j...@nospam.demon.co.ukwrote:
>
You're using javascript to implement a new OO language. Why?
That's a troll, but I admit I didn't properly explain my purpose up
front. See my other responses to Henry and Joost for a better
explanation of the purpose of the code.
>
* Helper function for creating a new object that
* inherits from some prototype object

<snip>

In other OO languages a function that creates new objects is called a
constructor, not a 'helper'.
In Javascript a constructor is a function intended to be called with
'new'. Functions that create objects but which aren't intended to be
invoked with 'new' are usually called factories. I could call this
function a factory instead of a helper, but it's just a comment so it
doesn't really matter.
Aug 13 '08 #8
carton a écrit :
(snip)
This is the classic debate about the need to enforce the boundary
between the interface and the implementation. The only alternative is
to trust users of your objects to 'follow the rules' and not access
properties that you may want to change. For small projects perhaps it
doesn't matter but for larger projects with multiple developers I
consider enforcement a necessity.
Python relies on a very simple naming convention to denote interface
from implementation, and it is proven by experience to JustWork(tm) even
on large projects with multiple developers.
Aug 14 '08 #9

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

Similar topics

3
by: Xamle Eng | last post by:
I am looking for a data oriented schema-enforcing XML editor. By schema-enforcing I mean an editor that doesn't just have a button to verify the schema - I want an editor that actively enforces it...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
9
by: gulu man | last post by:
Hi, What is the substitute for COM objects in .NET? How can I create something similar to com in .net? Is it still possible? Thank you
2
by: Frosty | last post by:
Howto make datagrid enforce rules of xml schema? Created xml schema in the designer. Constraints created there using the following <xs:simpleType name="zipcode"><xs:restriction...
19
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
10
by: ddtbhai | last post by:
Hello folks, Just wanted to know if there are some 'standard' approaches to enforcing an order in the invocation of calling functions. It is usually needed when initializing some object. e.g...
20
by: Chris | last post by:
I'm not sure if this has been done before, but I couldn't easily find any prior work on Google, so here I present a simple decorator for documenting and verifying the type of function arguments....
40
by: =?iso-8859-1?B?QW5kcuk=?= | last post by:
I'm really annoyed at Python - and not for the reasons already mentioned on this list. Everyone know that programming is supposed to be a dark art, nearly impossible to learn. Computer code is...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.