開源日報 每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,堅持閱讀《開源日報》,保持每日學習的好習慣。
今日推薦開源項目:《搭積木 tailwindcss》
今日推薦英文原文:《Can you tell me what a Class is?》

今日推薦開源項目:《搭積木 tailwindcss》傳送門:GitHub鏈接
推薦理由:興許在這之前你已經用過 bootstrap 這樣的 CSS 框架了——它為你提供了不少 UI 組件讓你直接拿來用,只需要一些微小的改動。但是如果你希望自己整一個有自己風格的網頁的話,這個 CSS 框架會適合你,它並沒有提供 UI 組件,但是給了你很多非常小的類來調整各種細節,你要做的就是把它們像搭積木一樣組合起來,直到你覺得滿意為止,這就是它最大的不同之處。
今日推薦英文原文:《Can you tell me what a Class is?》作者:Rob Parker
原文鏈接:https://medium.com/@into.the.parker/can-you-tell-me-what-a-class-is-61d8f61e284a
推薦理由:關於類的定義和工作方式

Can you tell me what a Class is?

….is the one of the questions I was asked during a phone interview last week.

A Class is something I see and use almost every day when programming, but when asked to describe what a Class is, I struggled a little. Which is interesting because you would think that something you see and use everyday would be easy to describe to someone. I guess I had never really had to think, and then describe in detail, what a Class is.

In my head I understood it and that was that….but in retrospect that was very foolish of me.
So what is a Class?

In Computer Science jargon a Class is described as the following.
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).In many languages, the class name is used as the name for the class (the template itself), the name for the default constructor of the class (a subroutine that creates objects), and as the type of objects generated by instantiating the class; these distinct concepts are easily conflated.
Pulled from Wikipedia, It』s a solid explanation, and I would be comfortable to give that in response to a question asking what a Class is.

So what if the next question was, what defines the behaviour or structure of a Class…

Let』s take a look at a Class in JavaScript.

This PhoneBook Class contains a constructor method to set the variables when the Class is instantiated and a getter and setter method.

Getter functions are meant to simply return (get) the value of an object』s private variable to the user without the user directly accessing the private variable.

Setter functions are meant to modify (set) the value of an object』s private variable based on the value passed into the setter function. This change could involve calculations, or even overwriting the previous value completely.

The behaviour of Class or its instances is defined using methods. Methods are subroutines with the ability to operate on objects or classes. These operations may alter the state of an object or simply provide ways of accessing it.

Let』s see how it all works.

Firstly we need to instantiate the Class to create an instance of the Class.

The syntax for Class creation very much depends on the type of Class and the contents of its constructor method.

In my example I create a variable named 「bob」 and using the 「new」 key word I assign it an instance of Phonebook passing through a name and phone number.


Pretty cool right? So we have a variable that contains an instance of the PhoneBook Class we now can access the data stored within the object with the get methods inherited from the Class.

It only gets getter…


By using dot notation we can access the get contactName and contactNumber getter methods by adding the variable name that defined in the constructor method.

Setters….


Very similar to the dot notation we used to access the data stored in our object variables we update the in same way but assign a value to the variable. This is only possible due the setter methods inherited from the Class.

Classes are awesome, and so is Object Oriented Programming. I would recommend checking out FreeCodeCamp for more content and tutorials on Classes and Objects.
下載開源日報APP:https://openingsource.org/2579/
加入我們:https://openingsource.org/about/join/
關注我們:https://openingsource.org/about/love/