vue.js学习笔记之prototype

本文介绍了JavaScript中prototype属性的两种使用方式:一是给构造函数的实例动态添加属性或方法;二是实现类间的继承,类似于Java语言中的继承机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


javascript中有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>


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值