Is 0. JS object-oriented?

In my opinion, JS is indeed an object-oriented language, but its mechanics are very strange. It’s certainly not a great language in the context in which it was born, but that doesn’t prevent it from having object-oriented characteristics: encapsulation, inheritance, polymorphism.

Today we will introduce the package of JS.

1. Definition of encapsulation:

Encapsulation, that is, hide the attributes and implementation details of the object, only public interface, control in the program to read and modify the access level of the attributes; Abstract data and behavior (or function) are combined to form an organic whole, that is, the data and the source code of operation data are organically combined to form a “class”, in which data and function are members of the class.

2. JS encapsulation

Some students may say, too simple, JS encapsulation is like this

/*e.g.1*/
var a = {};
a.name = 'Jack';
a.age = 18;
a.isRun = false;
a.run = function(){
  a.isRun = true;
  return a.name + ' is run now! '
}
a.stop = function() {
  a.isRun = false;
  return a.name + ' is stop now! '
}
Copy the code

And indeed, you can access its properties, its methods, through an OBJECT called A, which synthesizes its properties and its methods into a whole. But notice that this sentence combines the data from the abstraction with the behavior (or function) into an organic whole, where there is a keyword abstraction.

So, is ‘Jack’ abstract? Is it abstract?

Obviously everything here is representational, this is just an object. Is a language object oriented because it only has objects? That’s impossible.

So how do we achieve abstraction?

By the keyword new

3. Encapsulation of keyword new implementation

A quick word on the use of new

/*e.g.2*/
/*Person is a constructor*/
function Person(name, age){
  this.name = name;
  this.age = age;
  this.run = function(){
    this.isRun = true; // This is not very standard, should be announced in advance, but JS is also a dynamic language, a little lazy
    return this.name + ' is run now! '
  }
  this.stop = function() {
    this.isRun = false;
    return this.name + ' is stop now! '}}let Jack = new Person('Jack'.18) //Person {name: 'Jack', age: 18, run: ƒ, stop: ƒ}
Copy the code

Here we generate an object based on an abstract constructor.

The associated attributes and methods that a standard constructor should have are initialized or not, depending on how you use them, and are not required.

4. What does new return

Obviously, an object is returned. But this object is not quite the same as a normal object

/*e.g.1*/
a;/ / {name: "Jack", the age: 18, isRun: false, run: ƒ, stop: ƒ}
/*e.g.2*/
let Jack = new Person('Jack'.18) //Person {name: 'Jack', age: 18, run: ƒ, stop: ƒ}
Copy the code

Except for the isRun part, you notice that example 2 returns an extra Person. This thing right here is the constructor of this object. Every object has its constructor

Well, someone is going to say, “Nonsense, there is no constructor for a, you didn’t even use the keyword new, where did you get a constructor?”

I’m going to prove to you that A does have a constructor, but it’s going to involve a little bit of inheritance, so I’m not going to dwell on it too much, but I’ll just say it’s the default. Because object oriented is a closed loop knowledge chain, there will be problems that cannot be explained with existing knowledge in the process of understanding her. After hearing the closed loop, these problems will be solved easily.

/* continue with e.g.1 code */
// Add the method hello to the Object prototype and print the string hello
Object.prototype.hello = () = >{console.log('hello')}

a.hello()// hello
Copy the code

Card until tomorrow. End, scatter flowers!

Don’t start work! There’s a little bit of middle reasoning missing here. The missing piece is the link to the conclusion

  1. Without modifying the Prototype pointer, the double underscore proto of the object points to the Prototype of its constructor.
  2. Double underscore proto double underscore proto Properties and methods that start and end with a double underscore are not expected to be used and manipulated directly. This property is called implicit
  3. Prototype is a property that all methods have. Where is the top layer of all prototypes? On Function, Function’s implicit prototype chain points to Function’s prototype, and all method implicit prototype chains execute Function’s prototype. Object is a method. The prototype of Object is initialized by the interpreter. (You can skip it if you don’t understand)
  4. An object can call methods on its implicit stereotype chain unconditionally, accessing properties on the implicit stereotype chain.
  5. let b = new Object()A value of b{}

Here I add a property (method) to the Object prototype, and A can call this method

5. 为什么是this

Obviously, we’ll use this to construct the constructor. What the hell is this?

This: a scoped context.

Modify the code for e.g.2. Let’s see what this is

/*e.g.3*/
function Person(name, age){
  console.log(this)
  this.name = name;
  this.age = age;
  this.run = function(){
    this.isRun = true; // This is not very standard, should be announced in advance, but JS is also a dynamic language, a little lazy
    return this.name + ' is run now! '
  }
  this.stop = function() {
    this.isRun = false;
    return this.name + ' is stop now! '}}let Jack = new Person('Jack'.18) //Person {}
Person('Tom'.5) / / Window {Window: Windows,... }
Copy the code

As you can see, the new keyword where this points to an empty object points to the constructor. Without the keyword new, it points to the global window

So the new constructor here returns this in the constructor

conclusion

Here is a brief introduction to the role of the new keyword, the whole article is based on ES5 writing method, does not use the CLASS keyword provided in ES6. On the other hand, for the part of method encapsulation, we found that this behavior is actually more closely related to the constructed class, and not so much related to the implementation, so the method encapsulation will be further discussed in the inheritance chapter. At the same time, there is still a problem.

Encapsulation hides the attributes and implementation details of an object, exposes the interface only, and controls the level of access to read and modify attributes in the program

On the other hand, the constructor system based on this would also make encapsulation seem a little bit strange. These problems will be addressed in encapsulation (below).

So, see you next time