Skip to content

Instantly share code, notes, and snippets.

@lin
Last active June 1, 2019 07:53
Show Gist options
  • Save lin/14ddac587d1d922d27a1028cb42a4600 to your computer and use it in GitHub Desktop.
Save lin/14ddac587d1d922d27a1028cb42a4600 to your computer and use it in GitHub Desktop.
What do I mean when I talk about class?
// why we introduce class.
// this is a simple way to encapulate repeated patterns
// which is larger than the functions
// but as we can see that a class is a function underneath
// class = object + a construction function
let Object = require('./Object')
function createUser ( name, age ) { // the name and age is what special about this object
// what actuallly is important is that
// we use three piece of information to distinguish it from other.
// name age and User class itself.
// User class gives us more repeated construction
// we definitely can create the object in a way verbose way.
// but that is too time consuming and reduntant in terms of coding.
// so class in a way to reduce repetition. that's all.
// constructor
// instance attributes and methods
let user = {}
user.name = name
user.age = age
user.info = function(date){
return 'Name: ' + name + '; ' +
'Age: ' + age + ';' +
'Print Date: ' + date + '.'
}
// now we can assign some another object as its parent
// in this way we can attach more info into this created instance object.
user.parent = Object
}

Whenever you encounter a class, you know this is a large piece of repeated information.

The parameters you give it, you can distinguish this instance from other instances created from this class definition.

You know that by calling the constructor function, you created a hash object. they all share some similiarity.

The other thing about it is that some other things has been attached to it as its parent objects.

While all these techniques is simply for the purpose of reducing repetitions, nothing more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment