The core concept of surface Object Programming Technology: encapsulation, inheritance, polymorphism; In some major high-level programming languages, such as: c#,vb.net,java,php, etc. are easy to implement, and if you want to implement polygon object programming in JavaScript, It's not so straightforward and easy, because JavaScript is not an object-oriented language, so we can only simulate object-oriented programming through some JavaScript features, such as closures, prototype chains, and so on, which I think are the basis for mastering and using JavaScript flexibly. , there are a lot of javascript gurus in the garden that are introduced and analyzed in this area, and I am only re-understanding JavaScript Object-oriented points as a project leader (independently designed and developed web front-end and back-end).
Since it is object-oriented, we first need to know how to create an object, and here are a few common ways to create an object:
A. Create an object instance directly:
12345678910111213141516 |
//直接实例化一个对象
var Person1 = { Name:
"梦在旅途"
, Age: 29, Sex:
"男"
, Height: 178 }; alert(Person1.Name);
var Person2 =
new Object();
Person2.Name =
"梦在旅途"
;
Person2.Age = 29;
Person2.Sex =
"男"
;
Person2.Height = 178;
alert(Person2.Name);
//这个是上面的简写
var Person3 =
new Object({ Name:
"梦在旅途"
, Age: 29, Sex:
"男"
, Height: 178 });
alert(Person3.Name);
|
Pros: Create an object directly without having to define the type in advance;
Disadvantage: unable to achieve reuse;
B. Defining a post-instantiation object first:
12345678910 |
//先定义类,再实例化成对象
function Person4(n,a,s,h) {
this
.Name = n;
this
.Age = a;
this
.Sex = s;
this
.Height = h;
}
var p4 =
new Person4(
"梦在旅途"
, 29,
"男"
, 178);
alert(p4.Age);
|
Pros: Similar to object-oriented programming language constructors, easy to understand, and after the definition can be instantiated through the New keyword multiple objects, to achieve reuse.
Disadvantage: Need to define before you can instantiate;
In summary, it is recommended to use the B method to create objects.
Implement encapsulation, which exposes only public and public properties, and hides implementation details (private methods, properties)
123456789101112131415161718192021222324252627282930 |
function Person5(n, a, s, h) {
//公共属性
this
.Name = n;
this
.Age = a;
this
.Sex = s;
this
.Height = h;
//公共方法
this
.AfterYear =
function (count) {
updateAge(count);
alert(_currentYear +
"后,我已经:" +
this
.Age +
"岁了!"
);
};
this
.Say =
function () {
alert(
"我的个人信息--> Name: "
+
this
.Name+
", Age: "
+
this
.Age +
", Sex: "
+
this
.Sex +
", Height:" +
this
.Height);
}
//私有属性与方法
var _self =
this
;
var _currentYear = 2015;
function updateAge(count) {
_currentYear += count;
_self.Age += count;
};
}
var p5 =
new Person5(
"梦在旅途"
, 29,
"男"
, 178);
p5.AfterYear(10);
p5.AfterYear(25);
|
Using a prototype chain to implement inheritance, that is, one object contains all the public properties and methods of another object, there are many ways to implement inheritance, and I think the following form is used to simulate inheritance more consistent with object-oriented thinking:
123456789101112131415 |
function SoftEngineer(n, a, s, h, lang) {
Person5.call(
this
, n, a, s, h);
//将Person5的所有属性与方法包含到SoftEngineer中,从而实现继承
this
.Lang = lang;
this
.SayCode =
function () {
alert(
"我是一名软件工程师,我会" +
this
.Lang +
"编程语言!"
);
}
this
.Working =
function () { };
//空方法,类似面向对象中的虚方法
}
SoftEngineer.prototype =
new Person5();
//将SoftEngineer的原型指定Person5的实例
var softengr =
new SoftEngineer(
"梦在旅途"
, 29,
"男"
, 178,
"javascript"
);
softengr.Say();
softengr.SayCode();
|
The use of the prototype chain to achieve polymorphism, that is, based on the same method signature in different subclasses behave in different forms:
123456789101112131415161718192021222324 |
function WebSoftEngineer(n, a, s, h, lang) {
SoftEngineer.apply(
this
, [n, a, s, h, lang]);
this
.Working =
function () {
alert(
"我是网页工程师,从事网页开发设计工作!"
);
};
};
WebSoftEngineer.prototype =
new SoftEngineer();
function AppSoftEngineer(n, a, s, h, lang) {
SoftEngineer.apply(
this
, [n, a, s, h, lang]);
this
.Working =
function () {
alert(
"我是应用工程师,从事客户端应用程序开发设计工作!"
);
};
};
AppSoftEngineer.prototype =
new SoftEngineer();
var webengr =
new WebSoftEngineer(
"梦在旅途"
, 29,
"男"
, 178,
"javascript"
);
webengr.Say();
webengr.Working();
var appengr =
new AppSoftEngineer(
"梦在旅途"
, 29,
"男"
, 178,
"c#"
);
appengr.Say();
appengr.Working();
|
JavaScript must know: Surface Object programming