Skip to content

Instantly share code, notes, and snippets.

@Hoxtygen
Created July 1, 2020 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hoxtygen/4ba041f741c62483050f356e39f1c8b7 to your computer and use it in GitHub Desktop.
Save Hoxtygen/4ba041f741c62483050f356e39f1c8b7 to your computer and use it in GitHub Desktop.
// Object literals
let myObjectLiteral = {
variableKey: "variableValue",
functionKey: function (params) {
// magic goes in here
},
};
// module defined with object literal
var myModule = {
myProperty: "someValue",
//object literals can contain properties and methods.
//here, anothere object is defined for configuration purposes
myConfig: {
useCaching: true,
language: "en",
},
//a very basic method
myMethod: function () {
console.log("I can have something?");
},
//output a value based on current configuration
myMethod2: () => {
console.log(
"Caching is" + (this.myConfig.useCaching) ? "enabled" : "disabled"
);
},
//override the current configuration
myMethod3: (newConfig) => {
if (typeof newConfig === "object") {
this.myConfig = newConfig;
console.log(this.myConfig.language);
}
},
};
myModule.myMethod();
myModule.myMethod2();// undefined.
myModule.myMethod3({
language: "fr",
useCaching: false
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment