Last active
May 7, 2018 09:25
-
-
Save colevandersWands/93f774a0f3547a8bff74e3475f2f468c to your computer and use it in GitHub Desktop.
fancy model object with bind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all functions use the "this" key word | |
function create (new_value) {} | |
function read_one (ID) {} | |
function read_all () {} | |
function update (ID, new_value) {} | |
function remove (ID) {} | |
function binding_factory (instance_object) { | |
return { | |
create: create.bind(instance_object), | |
read_one: read_one.bind(instance_object), | |
read_all: read_all.bind(instance_object), | |
update: update.bind(instance_object), | |
remove: remove.bind(instance_object) | |
} | |
} | |
module.exports = binding_factory; | |
// --- new file | |
let binding_factory = require("./binding_factory") | |
let student_instance = { | |
db: {}, | |
nextID: 0, | |
name: "studnet" | |
} | |
let student_model = binding_factory(student_instance); | |
// https://goo.gl/3PGzHj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all functions use the "this" key word | |
function increment_id(){this.nextID++} | |
function get_id() {return this.nextID} | |
function binding_factory (instance_object) { | |
return { | |
increment_id: increment_id.bind(instance_object), | |
get_id: get_id.bind(instance_object) | |
} | |
} | |
let student_instance = { | |
db: {}, | |
nextID: 0, | |
name: "studnet" | |
} | |
let chair_instance = { | |
db: {}, | |
nextID: 0, | |
name: "chair" | |
} | |
let student_model = binding_factory(student_instance); | |
console.log(student_model.nextID) | |
console.log(student_model.get_id()) | |
student_model.increment_id() | |
console.log(student_model.get_id()) | |
let chair_model = binding_factory(chair_instance); | |
console.log(chair_model.nextID) | |
console.log(chair_model.get_id()) | |
chair_model.increment_id() | |
console.log(chair_model.get_id()) | |
// https://goo.gl/5Nu92o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment