JS中的对象
构造函数
function test([参数列表]) {
this.属性=属性值;
this.方法=function(){
…;}
}
var obj = new test(参数列表); // 使用new
工厂模式
function creatObject(name,age){
var obj= new Object( );
obj.name=name;
obj.age-age;
obj.run=function(){
return this.name+"------"+this.age;}
obj.say=function(){
…}
return obj; // 运行obj!!!
}
原型模式
function test( ){}
test.prototype.属性=属性值 ; prototype是object的子类
test.prototype.aaa=function(){};
var x99 = new test(); 需要new“赋值”才算成功创建
2.
test.prototype={
color:"red",
. aaaa:function(){},
…}
var x99 = new test();
混合模式声明
构造和声明
遍历对象 和存储
封装
private —私人调用接口
1.JavaScript中没有专门封装的关键词,可以使用闭包创建这种模式–函数内部声明的变量外部是无法访问的
2.能否被访问 取决于公有和私有内容区别 --能否在
继承
1.原型:利用prototype添加属性和方法
2.原型链 : proto (对象的内置属性)
var a = new b ();
a.proto = b.prototype
x98.prototype=new x97( ) {} ; 继承 原型继承对象属性
如果父对象和子对象同时具有相同的属性,输出时 子对象属性会覆盖父对象
在子类内部构造父类对象进行继承
function x96 () {
this.x969=x969
}
function x95( ) {
this.x959=x96; //继承 x96; 包括构造的所有属性和方法!!!
this.x969( x969); // 这里的意义????
call apply
obj.call(方法,var1,var2…)
obj.apply(方法,[var1,var2,…]
function
function student(name,age){
person.call(this,name,age)}
JavaScript 的多继承
function fun1( ) {
window.fun2.call(this);
window.fun3.apply(this,[“play”,“game”]);
}
function fun2( ) {
this.name=“玛莎拉蒂”;
this.run=function ( ) {
alert(“我跑的飞快!”);
}
}
function fun3(play,name){
this.play=play;
this.name=name;
}
var car = new fun1();
关键词
instanceof
delete // 变量不能删除 原型定义的方法不能删除
callee
this的引用
var x= 1;
function x94( ) {
this.x=0;
}
x94( );
alert(x); // 输出为0 函数执行后改变了全局
对象冒充 //见继承
将父类的属性和方法一起传给子类作为特权属性和特权方法
prototype 原型公有声明 对象冒充继承将不能实现
正则表达式 规则表达式 regular expression
正则表达式的模式匹配
模式:规则
匹配:作比较 校验(输入) 查找
正则表达式通常操作目标都是字符串
1.普通字符 (26个英文字母,数字)
2.特殊字符 例如 \ .
创建方式
1.字面量
/ reg /
var str+“I love js”;
var pattern= /js/gmi; //包含三个修饰符
模式修饰符 i 忽略大小写 ignoreCase
g global 全局匹配
m multline 对行进行匹配
console.log(pattern.test(str)) 返回bool
console.log(pattern.exec(str)); 返回数组
2.构造 更方便交互
new RegExp( );
var str=‘I love js’;
var pattern= new RegExp(‘js’,‘img’);