demo

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

demo2

  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>