Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Encapsulation generally refers to private data, but you could define classes that are just collections of methods, and you can define interfaces that are just collections of method signatures. An object which claims to support an interface can accept any of the messages defined by that interface, and another object can pass messages based on that.

A javascript example: in javascript, functions are objects, and can have methods.

    function MyFunction(...) { ... };
    MyFunction.method1 = function(...) {...};
    MyFunction.method2 = function(...) {...};
    MyFunction.method3 = function(...) {...};
Now, you can pass the messages 'method1', 'method2', or 'method3' to MyFunction. Message passing without encapsulation (as it's usually thought of.)

Of course, MyFunction has some default properties too, and you can define more:

    MyFunction.data1 = ...;
    MyFunction.data2 = ...;
That gives you traditional encapsulation, especially if you use somewhat more complex syntax to make the data private instead of public.


Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: