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

时间:2023-03-09 01:50:53
5月17日上课笔记-js面向对象
二、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; //实例属性
}