Saved Bookmarks
| 1. |
How Can Related Code Be Encapsulated? Give Example? |
|
Answer» The object literal is one of the SIMPLEST ways that the user can encapsulate related code together. It helps by removing any anonymous FUNCTIONS from the USERS code. It can also be used to CENTRALIZE CONFIGURATION options. For example: An object literal being implemented var myFeature={ myProperty : 'hello', myMethod : function() { console.log(myFeature.myProperty); }, init : function(settings) { m2yFeature.settings = settings; }, readSettings : function() { console.log(myFeature.settings); } }; myFeature.myProperty; // 'hello' myFeature.myMethod(); // logs 'hello' myFeature.init({ foo : 'bar' }); myFeature.readSettings(); // logs { foo :'bar' }The object literal is one of the simplest ways that the user can encapsulate related code together. It helps by removing any anonymous functions from the users code. It can also be used to centralize configuration options. For example: |
|