JavaScript对象及初始面向对象

JavaScript基本数据类型有:

number(数值类型)

string(字符串类型)

boolean(布尔类型)

null(空类型)

undefined(未定义类型)

创建对象

1.自定义对象

   var 对象名称=new Object();

基本运用:

 
 

var an=new Object();an.name="长春花";

an.show=function () { 

   alert(this.name);

}

an.show();

基本运用2:

var an={
    name:"产纯化",
    show:function () {
        alert(this.name)
    }
}

2.内置对象

String(字符串)对象.

Date(对象)对象

Array(数组)对象

Bollean(逻辑)对象

Math (算数)对象

RegExp对象

构造函数

使用构造函数创建新实例,必须使用new操作符,以这种方式调用构造函数实际上会经历以下四个步骤:

(1)创建一个新对象

(2)将构造函数的作用域给新对象(this 就指向了这个新对象)

(3)执行构造函数中的代码.

(4)返回新对象


基本运用:

 
 

function an(name,pwd) { 

  this.name=name; 

  this.pwd=pwd; 

  this.show=function () { 

      alert("用户名:"+this.name+"\n密码:"+this.pwd);

   }

}

var an=new an("name","123"); 

   an.show();

继承

基本运用:

function fu() {
    this.pwd=123;
    this.pwd2=456;
}
fu.prototype.getpwd=function () {
    return this.pwd;
}
function zi() {
    this.zipwd=12;

}
zi.prototype.getzi=function () {
    return  this.zipwd;
}
zi.prototype=new fu();
var zi=new zi();
alert(zi.pwd2);
组合继承
function fu(name,age) {
    this.show=function () {
        alert("姓名:"+name+"年龄:"+age);
    }

}
function zi(name,age,sez) {
    fu.call(this,name,age);
    this.sez=sez;
}
var zi=new  zi("nnn",20,'男');
zi.show();



猜你喜欢

转载自blog.csdn.net/qq_41941613/article/details/80011008