2020-03-26

创建对象的两种方法
1、构造函数创建对象
function student(name,age){
  this.name = name;      this.age = age;      this.say = function(){    console.log(“say something”); } this.action = function(){ console.log(“do something”); } } var xingoo = new student(“jack”,13);    console.log(jack.name);    console.log(jack.age);    jack.say();    jack.action();

2、初始化的方法创建
var person={
name:“名字”, age:13, say:function(){ console.log(“say something”); }, action:function(){ console.log(“do something”); } }; console.log(person.name); console.log(person.age);        person.say(); person.action();

2、面向对象:面向对象是一种通用思想,并非只有编程过程中使用,任何事情都可以使用面向对象。

3、 js中面向对象的三大特征:封装 继承和多态。
继承行:
function Animal(name,age){
      this.name = name;      this.age =age;      this.say = function(){ console.log(“animal say something”); }    }   function Cat(name,age){      Animal.apply(this,[name,age]);    }      var cat1 = new Cat(“jack”,3);      console.log(cat1.name);      console.log(cat1.age);      cat1.say(); console.log(Cat.prototype);
      
私有类:

      function demoFunc1(){        var privateFunc = function(){ console.log("this is privateFunc"); };         privateFunc();        this.objFunc1 = function(){ console.log("this is objFunc1"); };        demoFunc1.prototype.objFunc2 = function(){ console.log("this is objFunc2"); };                      }        demoFunc1.classFunc = function(){ console.log("this is classFunc"); };        demoFunc1.classFunc();         var f = new demoFunc1();        f.objFunc1();        f.objFunc2();

多态特性:子类会覆盖父类。
function Pig(name,age){
this.say = function(){ console.log(“i am pig”); }    }    Pig.prototype = new Animal();   function Dog(name,age){      this.say = function(){ console.log(“i am dog”); }    }   Dog.prototype = new Animal();  function say(animal){      if(animal instanceof Animal){ animal.say(); }    }    var dog = new Dog();    var pig = new Pig();    say(dog);    say(pig);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值