1、构造函数

构造函数:就是一个构造对象的函数
一类对象的抽象
特点:
1、函数名要大写
2、指向实例化的对象(谁new,指向谁)

  1. <script>
  2. /* 构造函数:就是一个构造对象的函数
  3. 一类对象的抽象
  4. 特点:
  5. 1、函数名要大写
  6. 2、指向实例化的对象(谁new,指向谁)
  7. */
  8. function Person(name,age){
  9. this._name = name;
  10. this._age = age;
  11. }
  12. var p = new Person("lu",19);
  13. var li = new Person("lisi",18);
  14. console.log(p);
  15. console.log(li);
  16. /*
  17. 构造对象(类) --学生 --一类事物的抽象
  18. 对象 --实际的对象 --类的实例
  19. */
  20. </script>

image.png

2、自定义函数方法

Student.prototype.Sayname = function(){
console.log(this.name)
}
Array 构造函数—数组类 /
/
在数组的原型上定义了一个say,那么所有的实例都会拥有一个say

  1. <script>
  2. function Student(name,age){
  3. this.name = name;
  4. this.age = age;
  5. }
  6. Student.prototype.Sayname = function(){
  7. console.log(this.name)
  8. }
  9. var p = new Student("李四",18)
  10. p.Sayname();
  11. </script>

image.png

  1. <script>
  2. Array.prototype.push = function(){
  3. console.log("失效辣")
  4. }
  5. var arr = [];
  6. arr.push(4);
  7. console.log(arr);
  8. /* remove(value) */
  9. Array.prototype.remove = function(value){
  10. var index = arr.indexOf(value);
  11. this.splice(index,1)
  12. return this
  13. }
  14. var arr = [5,6,7];
  15. console.log(arr.remove(6))
  16. /* sum */
  17. Array.prototype.sum = function(){
  18. var s = 0;
  19. for(var i=0;i<this.length;i++){
  20. s+=this[i];
  21. }
  22. return s;
  23. }
  24. var test = [1,2,3];
  25. console.log(test.sum())
  26. </script>

image.png
1、构造函数必须通过new关键字才可以调用
2、this指向实例化的对象

  1. <script>
  2. /*
  3. 1、构造函数必须通过new关键字才可以调用
  4. 2、this指向实例化的对象
  5. */
  6. function Student(name,age,skill){
  7. this.name = name;
  8. this.age = age;
  9. this.skill = skill;
  10. }
  11. Student.prototype.SayName = function(){
  12. console.log(this.name);
  13. }
  14. Student.prototype.SaySkill = function(){
  15. console.log(this.skill);
  16. }
  17. var p = new Student("李四",18,"编程")
  18. console.log(p);
  19. p.SayName();
  20. p.SaySkill();
  21. /*
  22. 对象
  23. 原型
  24. */
  25. </script>

image.png

3、模拟Stack栈

  1. <script>
  2. function Stack(){
  3. this.items = [];
  4. }
  5. /*
  6. 入栈 push
  7. 出栈 pop
  8. peek 获取栈顶
  9. isEmpty 判断栈是否为空
  10. size 可以看栈中有多少元素
  11. */
  12. Stack.prototype.push = function(val){
  13. this.items.push(val)
  14. }
  15. Stack.prototype.pop = function(){
  16. var res = this.items.pop();
  17. return res
  18. }
  19. Stack.prototype.peek = function(){
  20. return this.items[this.items.length-1];
  21. }
  22. var s= new Stack();
  23. s.push(2);
  24. s.push(3);
  25. console.log(s.items);
  26. console.log(s.peek());
  27. </script>

image.png

4、模拟进制转换

十转二:

  1. <script>
  2. /* 进制转换 */
  3. /* 十转二 */
  4. var num = 8;
  5. var arr = []
  6. while(num>0){
  7. arr.unshift(num%2)
  8. num = Math.floor(num/2);
  9. }
  10. console.log(Number(arr.join("")));
  11. </script>

image.png
二转十:

  1. <script>
  2. /* 二转十 */
  3. var num = 1010;
  4. str = (num+"").split("").reverse();
  5. console.log(str);
  6. var sum = 0;
  7. for(var i =0;i<str.length;i++){
  8. sum+=str[i]*Math.pow(2,i)
  9. }
  10. console.log(sum)
  11. </script>

image.png