美文网首页JavaScript < ES5、ES6、ES7、… >
JavaScript中原型的简单语法

JavaScript中原型的简单语法

作者: nomooo | 来源:发表于2019-03-17 17:37 被阅读20次
    简单的原型写法
        function Student(name, age, sex) {
          this.name = name;
          this.age = age;
          this.sex = sex;
        }
        //简单的原型写法
        Student.prototype = {
          //手动修改构造器的指向
          constructor:Student,
          height: "188",
          weight: "55kg",
          study: function () {
            console.log("学习好开心啊");
          },
          eat: function () {
            console.log("我要吃好吃的");
          }
        };
    
        var stu=new Student("段飞",20,"男");
        stu.eat();
        stu.study();
        console.dir(Student);
        console.dir(stu);
    
    原型中的方法是可以互相访问的
            //原型中的方法,是可以相互访问的
    
            function Animal(name,age) {
              this.name=name;
              this.age=age;
            }
            //原型中添加方法
            Animal.prototype.eat=function () {
              console.log("动物吃东西");
              this.play();
            };
            Animal.prototype.play=function () {
              console.log("玩球");
              this.sleep();
            };
            Animal.prototype.sleep=function () {
              console.log("睡觉了");
            };
    
            var dog=new Animal("小苏",20);
            dog.eat();
    
            //原型对象中的方法,可以相互调用
    
    实例对象使用的属性和方法层层的搜索

    实例对象使用的属性或者方法,先在实例中查找,找到了则直接使用,找不到则去实例对象的proto指向的原型对象prototype中找,找到了则使用,找不到则报错

        function Person(age,sex) {
          this.age=age;//年龄
          this.sex=sex;
          this.eat=function () {
            console.log("构造函数中的吃");
          };
        }
        Person.prototype.sex="女";
        Person.prototype.eat=function () {
          console.log("原型对象中的吃");
        };
    
    
        var per=new Person(20,"男");
        console.log(per.sex);//男
        per.eat();
        console.dir(per);
    
    把内置对象添加原型的方法
    
        //我们能否为系统的对象的原型中添加方法,相当于在改变源码
        //我希望字符串中有一个倒序字符串的方法
        String.prototype.myReverse=function () {
          for(var i=this.length-1;i>=0;i--){
            console.log(this[i]);
          }
        };
        var str="abcdefg";
        str.myReverse();
    
    
        //为Array内置对象的原型对象中添加方法
        Array.prototype.mySort=function () {
          for(var i=0;i<this.length-1;i++){
              for(var j=0;j<this.length-1-i;j++){
                  if(this[j]<this[j+1]){
                      var temp=this[j];
                    this[j]=this[j+1];
                    this[j+1]=temp;
                  }//end if
              }// end for
          }//end for
        };
    
        var arr=[100,3,56,78,23,10];
        arr.mySort();
        console.log(arr);
    
    
        String.prototype.sayHi=function () {
          console.log(this+"哈哈,我又变帅了");
        };
    
        //字符串就有了打招呼的方法
        var str2="小杨";
        str2.sayHi();
    
    把局部变量变成全局对象

    把局部变量给window就可以了

    
      //   函数的自调用---自调用函数
    
      //   一次性的函数--声明的同时,直接调用了
       (function () {
         console.log("函数");
    
    
       })();
    
      //   页面加载后.这个自调用函数的代码就执行完了
       (function (形参) {
         var num=10;//局部变量
       })(实参);
       console.log(num);
    
    
       (function (win) {
         var num=10;//局部变量
         //js是一门动态类型的语言,对象没有属性,点了就有了
         win.num=num;
       })(window);
       console.log(num);
    

    相关附加
    JavaScript原型
    JavaScript中原型的简单语法
    JavaScript原型链
    JavaScript中的继承

    相关文章

      网友评论

        本文标题:JavaScript中原型的简单语法

        本文链接:https://www.haomeiwen.com/subject/tcvumqtx.html