Day 23 : Become a self-taught blockchain developer with zero knowledge in 365 days

Ximena Elizeche
8 min readFeb 18, 2021

PREVIOUS: DAY 22

order 8: the fundamentals of Javascrpit (Technical aspect, Computer Science, Phase 1: a normal technican)

4.7 Symbol type

What I know about symbol type so far

  1. one of the 8 data types of Javascript
  2. like 7 data types except object type, it is primitive.
  3. the key of object can be string and symbol

SYMBOL PART 1: creation of symbol

  1. let id = symbol()

SYMBOL PART 2: the description of symbol

  1. let id = symbol("id") //the one with description which is optional, and mostly for debugging
  2. symbol is merely a label,affecting nothing
  3. each symbol is innately unique, two symbols with the same description are differenct values

SYMBOL PART 3: alert the symbol

  1. we are able to alert almost any value, but we are unable to alert symbol

2. in order to alert symbol, there are two ways. One is to explicitly call .toString. The other is to get symbol.description property.

method 1
method 2

SYMBOL PART 4: the use of symbol: Hidden properties

  1. as we mentioned above, symbol is one of the two types for the properties of the object, and the other is string. So there must be the difference of these two types.
  2. Both of two types are the key to access the data(object), but symbol is the hidden version of the properties of object, which means that no other part of the code of the object can access or overwrite the symbol.
  3. And not even the symbol with the same name but contain different values are able to affect other, which means that if I want to create somthing in the object's teritory, but I have no intention to amend the main code, sysmbol is the ideal wat for me to do so. Symbols are like the private space for each person, and only onself is aware of its existence.
  4. On the other hand, properie using string data would be replaced by one another with the same name.
  5. Technically, symbols are not 100% hidden. There are ways to find one.

SYMBOL PART 5: create the property using symbol in the object

  1. the use of symbol in the object must use [] instead of dot. For the string type, it has no such restriction, for the dot is only able to identify the string with no spaces
  2. What's noticeable is that [] is availabel for both string and symbol, and to identify thier difference, one must add the "",'' for strings.
  3. the key mentioned in the [] of the symbol is the symbol's variable, not its description (or it will become the string).

SYMBOL PART 5: not available for (for....in) loop

  1. symbol will be skipped for (for….in) loop
  2. it demonstrate its "hidden" trait of the object, it is undetectable by others

SYMBOL PART 5: not available for Object.keys(user)

  1. Object.keys(user) also ignores them
  2. If another script or a library loops over our object, it won’t unexpectedly access a symbolic property.
we want to clone the entire data, bbut it turns out that only th value is copied [id] is not.

SYMBOL PART 6: creating the exact same symbol: method 1

  1. we want the same-named symbols to be the same entities.
  2. the method 1: we create symbols in the global symbol registry, and it would be guaranteed that the symbols with the same names would have the same value when we try to access it.
  3. Symbol.for(key)
  4. the process: when we create a symbol in the global symbol registry, it would check if there is a symbol described in the same way, then returns it. Otherwise, it would create one. No matter what way it is, it wouln't affect the us.
  5. If we want an application-wide symbol, accessible everywhere in the code — that’s what they are for.
  6. the same symbol is accessible everywhere (like the nomal symbol of Ruby)
get the symbol by name

SYMBOL PART 6: find the key(name) of the symbol

  1. Symbol.keyFor(sym)
  2. the reverse call of Symbol.for(key), which is get the symbol by name. Here, we get the name by symbol.

3. the "Symbol.keyFor(sym)" only works for symbols in the global symbol registry if we want to look up the key of the symbol. Hence, . If the symbol is not global, it won’t be able to find it and returns undefined.

4. an example for the internal use of symbol

SYMBOL PART 7: system symbol

  1. there are many built in coommands related to symbol for us to use internally.
  2. Well-known symbols

4.8 Object to primitive conversion

Why we need to convert objects to primitives?

Object-to-Primitive Conversions in JavaScript, Object to primitive conversion

For the commomly used objects, the conversion of the type of the objects make you easier to write a clearer, cleaner, and it would be easier for debugging and logging purpose.

And when the type conversions is needed, the method would be pretty useful.

type conversion for objects

  1. three variants of type conversion, so-called “hints”
  2. all obejects are true in boolean. Hence, threre is no boolean conversions for object
  3. Strictly speaking, there is only two types, string and number.

type conversion for objects: type 1: the string hint

  1. style 1: we output an object, like "alert(obj)".
  2. style 2: anotherObj[obj] = 123; // using object as a property key

type conversion for objects: type 1: the number hint

  1. style 1: explict numeric conversion: let num = Number(obj);
  2. style 2: math operator(but expect the binary plus)

(1) unary plus: let n = +obj;

(2) normal math: let delta = date1 — date2;

3. style 3: the comparison: let greater = user1 > user2;

type conversion for objects: type 1: the default hint

  1. style1 +: the condition that the operator is unsure of the commands, such as binary plus. It is unable to distunguish the outcome is a string or a number.
  2. style 2==: if an object is compared using == with a string, number or a symbol, it’s also unclear which conversion should be done
  3. default are number can be trated the same. All built-in objects except for one case (Date object, we’ll learn it later) implement "default" conversion the same way as "number"

The method for the conversion: method 1: Symbol.toPrimitive

  1. the concept of method: A function that is assigned to the properties of the object
  2. I would just ignore the code itself, just focus on the concept of conversion. It's too complicated
  3. a built-in symbol
  4. user becomes a self-descriptive string or a money amount depending on the conversion.
  5. user[Symbol.toPrimitive] handles all conversion cases.

The method for the conversion: method 2: toString/valueOf

  1. the concept of method: A function that is assigned to the properties of the object
  2. an alternative for the times that symbol doesn't exist.
  3. Javascript find it in order, and returns a primitive order. If not, it's ignored. As if the method itself never exist
type conversion is ignored like this

4. the order: First, toString -> valueOf for “string” hint. Then, valueOf -> toString otherwise.

Return types

  1. the mandatory thing is these two methods of type conversion must return the primitive instead of the object.
  2. It will be the string or a number, we don't know about that
  3. Also, two methods have different reaction for the failure of coverting to primitive. Symbol.toPrimitive would be the failure, while toString/valueOf would just ignore it.

Other conversion: *

  1. the operators like * can do the type conversion of an object as well
  2. the process: There are three stages. First, use one of the method above. Second, the object is converted to the primitives. Third, if the resulting primitive isn't the right type, it's converted again.

Other conversion: +

  1. binary plus
  2. Binary plus will concatenate strings in the same situation, but what's differenct is, it gladly accepts a string

Data types

5.1 Methods of primitives

Methods for primitives

We already know that methods are the functions in the object. And we've all experienced the powerful functionality of methods. It would be unthinkable for using methods for primitives as well.

The evolvement of the language

For the function of the object, which is also the method, in spite of the convience, it occupies many spaces and resources to suport it. If there is methods for primitives, it would be a good thing. In that way, Javascript would be able to realize multiple functionalities with lightwieght.

The way to achieve methods for primitives: object wrappers

  1. The primitives remain the same, and the language allow them to access the method by creating the special “object wrapper”.
  2. This “object wrappers” is only for one-time use. It is destroyed after being used.
  3. The “object wrappers” are different for each primitive type
  4. ex: str.toUpperCase()
the speical object is created and have the access to str. Then, the method runs and returns the result. After that, it initiates self-destruction and the variable is left alone.

object wrappers for contructor function is not recommended

  1. new Number(1) or new Boolean(false).
  2. it's valid
  3. become more cofusing and complex.

null/undefined have no methods

  1. They have no corresponding “wrapper objects” and provide no methods.

#

order (4): Introduction to the blockchain technology by Ethereum, developer perspective

I would try to fully understand the followings from here.

Fundamental topics

  • Intro to Ethereum — A quick overview of Ethereum #
  • Web2 vs Web3 — The fundamental differences that blockchain-based applications provide#
  • Intro to dapps — An introduction to decentralized applications#
  • Accounts — Entities in the network that can hold a balance and send transactions #
  • Transactions — Transfers and other actions that cause Ethereum’s state to change#
  • Blocks — The way transactions are batched to ensure state is synchronised across all actors
  • The Ethereum Virtual Machine (EVM) — The EVM handles all the computation on the Ethereum network
  • Block explorers — A way to read data about transactions, accounts, blocks and the network
  • Gas — Computational power required to process transactions, paid for in ETH by transaction senders
  • Mining — The way transactions are processed and blocks are created
  • Networks — Implementations of Ethereum including test networks
  • Nodes and clients — The individuals participating in the network and the software they run to verify transactions

###Done for the first five###

#

NEXT: DAY 24

--

--