vue学习笔记之vue.prototype

一、prototype可以给对象动态的添加属性或者方法,如:

<script>
    function student(stuname,age,classname){
        this.stuname = stuname;
        this.age = age;
        this.classname = classname;
    }
    var stu1 = new student("yovan",21,"软件七班");//实例化学生对象
    student.prototype.php_score = null;//利用prototype给student中添加一个php_score属性
    stu1.php_score = 99;
    console.log(stu1.php_sorce);//99
</script>
二、prototype可以做到类似Java继承那样,如:
<script>
    function mytest1(parameter){
        this.testNum = parameter;
   };
   function mytest2(parameter){
        this.testString = parameter;
   };
    //用mytest2的prototype去实例化mytest1,继承了mytest1里面的testNum属性
    mytest2.prototype = new mytest1(2017); 
    var objectTest2 = new mytest2("hello");
    console.log(objectTest2.testString+","+objectTest2.testNum);//hello,2017
 
</script>

三.

Vue.prototype.funcName = function (){}
Vue.prototype.$axios=axios;    //代码解读

在main.js里进行全局注册axios()方法,使其可以在所有组件里通过this.funcName();调用

猜你喜欢

转载自blog.csdn.net/qq_41063141/article/details/82934905
今日推荐