Notes object-oriented programming

For the base face of the object,

What is the face of the object programming thinking that, it is that every object has his unique methods, and properties. The idea is that the program, I never mind the principle of the method you than just hair dryer, hair can be, I took a hair, not to manage how could he blow the wind. As long as know how to use on the line!

Method of the object, to know what's the use, when you need to take over with a fine!

Method to create an object!

Object literal way to create more common!

It can be seen every time the number of objects to create a new look that we all need to reduce the new factory mode

Factory mode is also a drawback of both

Each object has its own set of methods, waste of resources (although for now the computer is nothing, but we will try to do the best design trying to be difficult.
Why here that every object has its own set way to do that, because creation function () when its essence is through new Function () to create,
Will be the birth of a new function object, the method of each object is not the same, so there is a waste of resources problem.
 
Like on str = function () {} = str = new function () {}
 
There is a constructor mode

They have their own advantages and disadvantages, look at the right place to use.

There is a prototype to create objects (this is the combined configuration of inheritance and prototype inheritance, which I'll cover below)

Because here are the return of a call, therefore, it is undefined, so the comparison is true  

Programming Exercises array of small sums

Basically based on these

Then I just encountered this problem on the class,

What class is? What is class? Class is the method? What kind is it?

JS in fact, there is no concept of class, a new class is called analog out. Especially when we are using new time, it makes the concept of "class" is more like a class in other languages.

Inheritance is a class type to call the parent class's constructor function in the subject, such that their access methods and properties of the parent class. call and apply methods provide support for the class type inheritance. This action by changing the environment, so that the subclass itself have various attributes of the parent class.

Class can be a function, such as the above d is the constructor of a class.

There is one thing in common, he is the prototype of all classes have a property;

Three attributes of the classes, inheritance, abstraction, polymorphism;

继承:创建一个或多个类的专门版本类方式称为继承(Javascript只支持单继承)。 创建的专门版本的类通常叫做子类,另外的类通常叫做父类。

在Javascript中,继承通过赋予子类一个父类的实例并专门化子类来实现。在现代浏览器中你可以使用 Object.create 实现继承.(出自 MDN web docs javascript面对对象编程);

类式继承

原型继承在开发中经常用到。它有别于类继承是因为继承不在对象本身,而在对象的原型上(prototype)。每一个对象都有原型,在浏览器中它体现在一个隐藏的__proto__属性上。在一些现代浏览器中你可以更改它们。比如在zepto中,就是通过添加zepto的fn对象到一个空的数组的__proto__属性上去,从而使得该数组成为一个zepto对象并且拥有所有的方法。话说回来,当一个对象需要调用某个方法时,它回去最近的原型上查找该方法,如果没有找到,它会再次往下继续查找。这样逐级查找,一直找到了要找的方法。 这些查找的原型构成了该对象的原型链条。原型最后指向的是null。我们说的原型继承,就是将父对像的方法给子类的原型。子类的构造函数中不拥有这些方法和属性。

另外的一种模式,是结合类继承和原型继承的各自优点来进行对父类的继承。用类式继承属性,而原型继承方法。这种模式避免了属性的公用,因为一般来说,每一个子类的属性都是私有的,而方法得到了统一。这种模式称为组合模式,也是继承类式常用到的一种方法。

在上面的时候有写过。

抽象:抽象是允许模拟工作问题中通用部分的一种机制。这可以通过继承(具体化)或组合来实现。
JavaScript通过继承实现具体化,通过让类的实例是其他对象的属性值来实现组合。

 

多态:就像所有定义在原型属性内部的方法和属性一样,不同的类可以定义具有相同名称的方法;方法是作用于所在的类中。

并且这仅在两个类不是父子关系时成立(继承链中,一个类不是继承自其他类)。

 

 

new 和  Obeject.create

 “new关键字掩盖了Javascript中真正的原型继承,使得它更像是基于类的继承。

其实new关键字只是Javascript在为了获得流行度而加入与Java类似的语法时期留下来的一个残留物”。作者推荐我们使用Object.create方法创建或者实例化对象。露珠做过测试,使用new和使用object.create方法都是将对象添加到原型上去

在使用  Object.create方法创建属性和方法都在原型上。

var e = {

   a:  'father' ,
   b:  function () {
      alert( this .a);
    }
}
 
var  obj = Object.create(e);
console.dir(obj);

 

 

 

 

 

 

 
 

 

Guess you like

Origin www.cnblogs.com/chenyudi/p/11068349.html