Packaging
<script language= "JavaScript" >
function Person (name,age,sal) {
this.name=name;//Public Properties
This.age=age;
This.sal=sal;
sal=sal;//Private Properties
How to define public methods (privileged methods) and private methods
If we want to manipulate private properties, use the public method
This.show=function () {
Window.alert (Age+sal)
}
Private methods that can access the properties of an object (can only be used inside a class)
function Show2 () {
Window.alert (Age+sal)
}
}
var p1=new person (' SP ', 34,5000);
P1.show ()
</script>
Add methods to all objects through prototype, but this method cannot access the private methods and properties of the class through the previous study
function P1 () {
this.a=1;//Public method
var age=20;//private method
}
P1.prototype.func1=function () {
Alert (THIS.A)
Alert (age)
}
var a=new p1 ();
A.func1 ();
Nanyi prototype
Inherited
<script language= "JavaScript" >
function Stu (name,age) {
This.name=name;
This.age=age;
This.show=function () {
Alert (This.age+this.name)
}
}
function M1 (name,age) {
This.stu=stu;
This.stu (Name,age),//js in fact through the object impersonating, to inherit, this sentence can not be missing
M1 can override the show method of the Stu parent class, overloaded with overrides (overwrite)
This.show=function (qian1) {
Alert (QIAN1)
}
}
function M2 (AA,BB) {
This.stu=stu;
This.stu (AA,BB);
}
var m1=new M1 (' cc ', 30)
M1.show ()
</script>
JS can be multiple inheritance, does not support overloading (can not be determined by the number of parameters to call which function, but because
JS natural can support variable parameters, so it can see the natural support overload)
Function abc () {
If argument.length==1{
}
}
Overriding: Subclasses can re-implement a method to overwrite a method of a parent class
function Stu (name,age) {
This.name=name;
This.age=age;
This.show=function () {
Alert (This.age+this.name)
}
}
function M1 (name,age) {
This.stu=stu;
This.stu (Name,age),//js in fact through the object impersonating, to inherit, this sentence can not be missing
M1 can override the show method of the Stu parent class, overloaded with overrides (overwrite)
This.show=function (qian1) {
Alert (qian1+ ' rewrite ')
}
var m1=new M1 (' cc ', 30)
M1.show ()//This will re-invoke the overridden method
Polymorphic
Multi-state case
Master Class
function Master () {
To eat animals
This.feed=function (Animal,food) {
document.write (' master gives ' +animal.name+ ' food is ' +food.name ');
}
}
Food Products
function food (name) {
This.name=name;
}
function Fish (name) {
This.food=food;
This.food (name)
}
function Bond (name) {
This.bond=food;
This.bond (name)
}
Animals
function Animal (name) {
This.name=name;
}
function Cat (name) {
This.cat=animal;
This.cat (name);
}
function Dog (name) {
This.dog=animal;
This.dog (name);
}
var cat=new cat (' small white ')
var dog=new dog (' Little black ')
var fish=new fish (' shark ')
var bond=new bond (' warrants ')
var m=new Master ()
M.feed (Cat,fish)
M.feed (Dog,bond)
Closure definition
1: Related to GC
2: Closed actually involves the member property of an object, when it is processed by the GC
3: How can I form a closure on an object's properties?
function A () {
var i=0;
Function B () {
alert (i++);
}
return b;
}
A ();//This time in-memory I space is processed by GC
var c=a ();//This usage, GC does not treat I as garbage
c (); Output 0
C () Output 1, which proves that the variable is closed
System
Inner class
Object,array/math/boolean/string/regexp/data/number
Example
Math, Static class
Alert (Math.Abs (-29))
Show current date, dynamic class
var not1=new Date ();
Alert (not1.tolocaledatestring ())
JavaScript (JS) Foundation 3:.js Object-oriented three major features (encapsulation, inheritance, polymorphism) and inner classes. JS System functions