The object-oriented development of js is actually object-based development (js is the concept of no tiredness);
1: Any object-based transformation has three characteristics, abstract encapsulation, inheritance, and polymorphism.
1 Abstract encapsulation: Abstraction refers to extracting the attributes and behaviors (methods) of objects, and then combining them organically. This process is called encapsulation. The result is that only the interface is provided externally (the common attributes and common methods are created through this), while the internal implementation ( Public methods can call private methods).
2: Inheritance: The process in which a subclass implements the properties and methods of the parent class is called inheritance; the commonly used inheritance is as follows.
1: call or apply
function F(x){
this.x=x
this.sayX=function(){
alert(this.x)
}
}
function Y(x){
F.call(this,x);
//F.apply(this,agruments);
}
var t=new Y(2);
alert(t.sayX())//2
Note⚠️: call apply cannot be called
function F(x){
this.x=x
}
F.prototype.sayX=function(){
alert(this.x)
}
function Y(x){
F.call(this,x);
}
var t=new Y(2);
alert(t.sayX())//new_file.html?__hbt=1524886368955:91 Uncaught TypeError: t.sayX is not a function
2 Prototype chain inheritance
function Person(name, sex){
this.name=name;
this.sex=sex;
}
Person.prototype.showName=function(){
alert(this.name);
}
Person.prototype.showSex=function(){
alert(this.sex);
}
function worker(name,sex,job){
this.job=job;
Person.apply(this,arguments)//获取公有的属性
}
worker.prototype=new Person()
worker.prototype.sayJob=function(){
alert(this.job)
}
var n=new worker('xxw','nan','chenguxyuan');
console.log(n)
alert(n.showName())//xxw
alert(n.showSex())//nan
alert(n.sayJob())//chenguxyuan
3 多态
js 天然支持多态度
通过判断参数argumrnts还有若数据类型,赋值什么数据就是什么数据