林大妈的JavaScript基础知识(三):JavaScript编程(3)原型

  • 2019 年 10 月 3 日
  • 笔记

????????????????????????????????????JavaScript???????????????????JavaScript?????????????????????????????????????????????????????????????????JavaScript?????????????????????JavaScript????????????????????????????????????

 

1. ?????

????????????“??”??????????????????????????????__proto__????????????????????prototype????????????????????????????????????????????????????????????JavaScript?????????????????????????????????????????????????????????????????????????

1 var MotherLyn = function(generation, name) {  2     this.generation = generation;  3     this.name = name;  4 }  5  6 MotherLyn.prototype.show = function() {  7     return this.generation + this.name;  8 }

?????????????????????generation?name??????????????????show?????constructor??????????????????????????????????????Object??????????????????????????????????????Object????????????

2. ???

??????????????????????????????????????????????????????

????????????????Person Prototype???????Person????????????????person??????????????Person Prototype????????????????????????????????

?????????????[[Prototype]]??????prototype??????[[Prototype]]??????????????????person????????????????????????__proto__???????????????__proto__???[[Prototype]]?????????prototype??????????????????????????????????????

??????????????????????????????????????????????????????__proto__????prototype??????????????????????????????????????????????????????Person?????????prototype????Person??????????????????Function.prototype???????????__proto__????Function?????????????????????????????????????????????????????????????????????????

??????????????????Person Prototype?????????????????Object.prototype??????JavaScript?????????????toString???hasOwnProperty????????????????????????????????????????????????????????????Object.prototype?????Object.prototype???????null??????????????????????????????????????????????????????????????

?????JavaScript????????????????? ???????????? ???????????? ? ??????????????? ……? ????????Object???????????????????undefined??????????

 

3. ???????????

???????????????????????????????????????????????Object?Array?Function????????????????Array??????????????

 1 if(!Array.prototype.shuffle) {   2     Array.prototype.shuffle = function() {   3         for(var i = this.length, j = Math.floor(Math.random() * i), x; i; j = Math.floor(Math.random() * i)) {   4             x = this[--i];   5             this[i] = this[j];   6             this[j] = x;   7         }   8         return this;   9     }  10 }

?????????????????.prototype????????????????????Function????????????????

1 if(!Function.prototype.method) {  2     Function.prototype.method = function(name, code) {  3         if(!Function.prototype[name]) {  4             this.prototype[name] = func;  5             return this;  6         }  7     }  8 }

???????????????method?????????????????????????????????????

 

4. ???????????

???????????????????????????????????????????????????

????? ???Employee????????????????????name?????salary??????show????????name?salary?

????? ???Manager????????????????????????????????inferiors???????getInferiors????????inferiors?

????? ???Secretary???????????????????????????????superior????????????????getSuperior????????superior?

?????????????

1 function Employee (name, salary) {  2     this.name = name;  3     this.salary = salary;  4  5     this.show = function () {  6         return this.name + ": $" + this.salary;  7     }  8 }

??????????????

 1 function Manager (name, salary, inferiors) {   2     Manager.prototype.name = name;   3     Manager.prototype.salary = salary;   4     this.inferiors = inferiors;   5   6     this.getInferiors = function () {   7         return this.inferiors;   8     }   9 }  10  11 function Secretary (name1, salary1, name2, salary2) {  12     Secretary.prototype.name = name1;  13     Secretary.prototype.salary = salary1;  14     this.superior = new Manager(name2, salary2);  15  16     this.getSuperior = function () {  17         return this.superior;  18     }  19 }

????????????????????????????????JavaScript?????????????C++?Java???“?”?????????????????????????????????????

1 Manager.prototype = new Employee();  2 Secretary.prototype = new Employee();

 ???????manager?secretary??????????__proto__????????Employee????

 

?????? ??????????JavaScript???????????????????????????“??”?????????????????????for in???????????????????

?????? ??????????????????Object.prototype?Object.prototype????null?????????????????JavaScript?????????????????????????????????????undefined?

?????? ???????????JavaScript???????????????????

?????? ????????????????????????????