码迷,mamicode.com
首页 > 编程语言 > 详细

javascript对象

时间:2015-05-14 18:07:48      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

一.类型

五种基本类型:

  nullundefinednumberbooleanstring

  null表示没有声明,undefined表示声明后没有初始化,其余三个有对应的包装对象NumberBooleanString

对象类型:

  object,比如常用的ArrayDateRegExp等,我们最常用的Function也是个对象

二.类型识别函数

  验证类型  typeof 对象名;  输出对象类型

三.javascript对象

  1.声明对象

  //用对象的方法声明对象

    var newobj = new Object();
    newobj.name = "jim";
    newobj.method = function(){
      console.warn(this.name);
    }
  //用字面值方法构造对象
    var obj = {
      name:"oliver",
      method:function(){
        console.warn(this.name);
      }
    }
  //使用方法来创建多个变量
    function createObj(name){
      var obj = {
        name:name,
        method:function(){
          console.warn("demo");
        }
      }
      return obj;
    }
  //定义构造方法
    function Person(name,age){
      this.name = name;
      this.age = age;
        this.method = function(){
        console.warn(this.constructor);//指向该对象的构造器function Person(name,age){}
        console.warn("new person");
        }
      //任何函数使用new运算符就是构造函数
    }

  2.对象间关系

  function Monkey(gender){

    this.gender = gender;
    this.method = function(){
      console.warn(this.gender);  
    }
  }

  
function Person(name,age){
    this.name = name;
    this.age = age;
    this.prototype = new Monkey("male");//用另外一个构造函数赋值原型
    this.method = function(){
      console.warn(this.prototype);
    }
  }

  $(function(){
    $(".proto").click(function(){
      var p = new Person("jim",20);
      p.method();
    });
  });

 

javascript对象

标签:

原文地址:http://www.cnblogs.com/brolanda/p/4503753.html

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