Skip to content

Instantly share code, notes, and snippets.

@lin
Last active May 30, 2019 02:57
Show Gist options
  • Save lin/25bbead717920db4b63e1b26e2014945 to your computer and use it in GitHub Desktop.
Save lin/25bbead717920db4b63e1b26e2014945 to your computer and use it in GitHub Desktop.
Understanding by implementing it in another way.
let user = { name: "John" };
let speak = function (price) {
console.log(this.name + ' offers ' + price)
}
function dot_object_call(obj, fun, args){
fun.call(obj, args)
// or:
// fun.bind(obj)(args)
}
dot_object_call(user, speak, '$2.99')
// same as
// user.speak = function (price) {
// console.log(this.name + price)
// }
// user.speak('$2.99')
// In a method call, `this` is always the object before the dot.
// Constructor functions or, briefly, constructors, are regular functions
// Constructor is a function to construct a type of object by feeding it with specific details
// new_user('Albert') function returns
funciton new_user(name) {
let user = {}
user.name = name
user.speak = function(){
console.log(this.name)
// can't be name, cause you want it to be flexible
// this is the speciality of object. the attributes are dependent on each other, rather independent information
// it can be understand as
// user.speak = function(){
// let currentUser = caller_of_the_function // provided by the function
// console.log(currentUser.name)
// }
}
return user
}
function User(name) {
this.name = name;
this.isAdmin = false;
}
function new_object_from_function(fun, args){
let thisObj = {}
fun.bind(thisObj)(args)
this.__proto__ = fun.prototype
// in here, fun.prototype = User
// set in User, that User.prototype = { constructor: User }
return thisObj
}
new_object_from_function(User, 'jack')
// same as
// new User('jack')
let animal = {
eats: true
};
// create a new object with animal as a prototype
let rabbit = Object.create(animal); // same as rabbit.__proto__ = animal
Object.getPrototypeOf(rabbit); // rabbit.__proto
Object.setPrototypeOf(rabbit, {}); // rabbitl._proto = {}
Object.create = function(o){
function F(){}; // an internal F as a constructor of a new object
F.prototype = o; // this constructor has o as its prototype, that means objects created by this constructor has
// the __proto__ as o
return new F(); // create one object
}
// `Object.create` take an object as the prototype of constructor, which in turn is the __proto__ of the newly created object.
o = {};
// is equivalent to:
o = Object.create(Object.prototype);
function Object(props) { // pure construction function. assign the fields and methods of the object
this.name= props.name
this.run = function(){}
}
Object.assign = function(){} // static class level function
// an object constains all the needed information for a constructor
Object.prototype = { // object.__proto___, object can access the method too.
// shared by all objects, whereas in constructor, the method is specifically for instance
// which means the value of the data or function is depended on the input you give.
constructor: Object,
toString(){}
valueOf() {}
}
// how to create
Object.create = function(o){
function F(){}; // an internal F as a constructor of a new object
F.prototype = o; // this constructor has o as its prototype, that means objects created by this constructor has
// the __proto__ as o
return new F(); // create one object
}
let obj = {}
obj.__proto__
// means what is the parent of obj, or what x inherits from.
// this is true for all the objects, default parent is Object.
// by inherits, object can access parent's method.
function F(){}
F.prototype
// for all the function, it can work like a constructor for an object.
// to invoke it, you can type `let f = new F()`
// F will create an object and assign it to f.
// Additionally, F will also provide a parent for f, which is F.prototype
// F.prototype is create while new F() invoked.
// the default of that parent is {constructor: F} object, nothing more.
// in this way f.__proto__ will be {constructor: F}. to remember that it's created by F.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment