0%

My First Post

Hey

这是我的第一篇博客。

preface

In the first blog I want to talk about methods of primitives.We know objects have methods and in javascript we have the power to treat them as they were objects.

first why we want to treat primitives like objects

There are many things one would want to do with a primitive, like a string or a number. It would be great to access them using methods.


We should know the respective specialities of primitive and object before we want to combine the speciality in object(have methods) into the mechanism of primitive

the distinctions between primitive and object

primitive

is a value of the basic type string, number, bigint, boolean, symbol, null and undefined.
So it’s light-weight.

object

object is capable to store mutiple values as their “properties”

there is a superiority of object that is : we can insert or store a function into a property

1
2
3
4
5
6
let object = {
name: "jack",
sayhi: function () {
alert(this.name);
},
};

So Objects are “heavier” than primitives. They require additional resources to support the internal machinery.

Then we manage to add the speciality into primitive which is being able to use “.” to invoke some methods.

How to accomplish that : treat primitives like objects

With the speciality added,

we also want to keep primitives lightweight In the meantime.

So here we think :Only When we want to operate primitives with methods they are objects And When we don’t need that they change back to primitive (So that they can provide methods, but they still remain lightweight)
So flexible!

In order to do that :a special “object wrapper” that provides the extra functionality is created, and then is destroyed.

Dive in

1
2
3
let str = "Hello";

alert( str.toUpperCase() ); // HELLO
  1. The string str is a primitive. So in the moment of accessing its property, a special object is created that knows the value of the string, and has useful methods, like toUpperCase().
  2. That method runs and returns a new string (shown by alert).
  3. The special object is destroyed, leaving the primitive str alone.

Which means something besides the simple and standalone value is actually created!

Some details to supplementary

  1. null/undefined have no methods
    In a sense, they are “the most primitive”.

  2. using the same functions String/Number/Boolean without new is totally fine and useful thing. They convert a value to the corresponding type: to a string, a number, or a boolean (primitive).

    For example, this is entirely valid:

    1
    let num = Number("123"); // convert a string to number
  3. Still !! primitives are not objects. which means they can’t store additional data

1
2
3
4
5
let str = "Hello";

str.test = 5;

alert(str.test);

the result will be

  1. undefined (no strict mode)
  2. An error (strict mode).

The reason is that “wrapper object” :
when you use “.” after “str” (in other words when you wanna access the “str”) the “wrapper object” is created.

  1. in “no strict mode” it carries the “test” property but remember it will disappear When you see the “;” after”5;” it disappears. So you won’t have the trace of the “wrapper object” .It vanishes with no trace. So you could only get “undefined”
  2. in “strict mode” : there is a red flag or a Rule that cannot be crossed : you can’t write into “wrapper object” So An error