码迷,mamicode.com
首页 > Web开发 > 详细

5月17日上课笔记-js面向对象

时间:2017-06-21 11:45:44      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:call   继承   逻辑   prototype   alert   属性   str   ons   test   

二、js面向对象
    js创建对象:
        var 对象名称 = new Object();
        person.name = "小明"; //姓名
        person.age = 18;
        person.location = "合肥";
        person.showName = function(){
            alert(this.name);
        }
        //调用属性
        //alert(person.name);
        person.showName();
    字面量创建对象:
        JSON格式
            var person={
                name:"",
                age:16
            }
    内置对象:
        String(字符串)对象
        Date(日期)对象
        Array(数组)对象
        Boolean(逻辑)对象
        Math(算数)对象
        RegExp对象
            var reg = /^1[34587]\d{9}$/;  //正则表达式
            reg.test(value); //验证
    构造函数:
        //构造方法
        function Person(name,age,location,score){
            this.name = name;
            this.age = age;
            this.location = location;
            this.score = score;
            //成员方法
            this.show = function(){
                alert(this.name+"--"+this.age);
            }
        }
    构造方法属性:
        对象.constructor
        
    原型对象(类似父类)
    原型链(类似java的多重继承)
        Man.prototype = new Humans();
    借用构造函数实现继承:
        function Humans(){
            this.sex = "男";
            this.name="大黄";
        }
        function Man(){
            //借用构造函数实现继承
            Humans.call(this);   //继承了Humans,同时还传递了参数
            this.age=38;              //实例属性
        }
        

 

5月17日上课笔记-js面向对象

标签:call   继承   逻辑   prototype   alert   属性   str   ons   test   

原文地址:http://www.cnblogs.com/benben2013A/p/7058079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!