面向对象

构造函数

function person(name,sex,age){
	this.name = name;  //this.name属性     name参数 习惯上 属性名称==参数
	this.sex  = sex;
	this.age  = age;
	this.show = function(){
		alert(this.name+"---"+this.sex+"---"+this.age);
	}
}
var obj1 = new person("zhangsan","nan",18);
//alert(obj1.sex);
obj1.show();
var obj2 = new person("lisi","nv",20);
obj2.show();
//注意: this 代表当前对象,obj1和obj2两者之间是独立的,函数内部只能用this访问属性和方法

工厂模式

function createObject(name,age){
	var obj = new Object();
	obj.name = name;// obj.name 属性  name参数
	obj.age  = age;
	obj.run = function(){ //在obj对象中 调用obj对象的属性 this 代表的当前对象***
		return this.name +"----" + this.age +"运行中....";  
	}
	obj.say = function(){
		return "今天天气不错";
	}
	return obj;
}
var box1 = createObject("张三",18);
//alert(box1.name); //调用属性成功
//alert(box1.run()); //调用方法成功
var box2 = createObject("李四",20);
//alert(box2.name);
alert(box2.run());
//box1 和 box2 有关系???
// 构造和 工厂模式 不同:
//1 构造方式不会显示创建对象 将属性赋值给 this ,不需要return 对象
//2 工厂 在方法内部创建 object对象  返回object对象 ,属性和方法都是赋给object对象

原型模式

function test(){
}
//alert(test.prototype instanceof Object); //自带该对象 prototype是Object子对象
test.prototype.color 	= "red";
test.prototype.heights 	= "1.7";
test.prototype.widths 	= "1.2";	
test.prototype.showInfo = function(){
	alert(this.color+"---"+this.heights+"---"+this.widths);
}
test.prototype.getinfo  = function(){
	alert("aaaaa");
}
var car1 = new test();
car1.getinfo();


function test(){
}
//json数据定义属性和方法
test.prototype={
	color:"red",
	heights:"1.7",
	widths:"1.2",
	showinfo:function(){
		alert(this.color+"---"+this.heights+"---"+this.widths);
	},
	getinfo:function(str){
		alert(str);
	}
}
var car1 = new test();
car1.getinfo("abc");

混合模式

//混合模式:构造+原型
function blog(name,url,friend){
	this.name 	= name;
	this.url	= url;
	this.friend	= friend;
}
blog.prototype={
	test:"awt",
	showinfo:function(){
		alert(this.name+"---"+this.url);
	},
	gets:function(){
		alert(this.friend);
	}
}
var peo = new blog("张三","http://www.baidu.com","李四");
peo.showinfo();

封装

function demo() {
  var n = 1; //局部变量,在方法外部不能直接访问
  function test() { //外部调用的出口
    n++;
  }
  test(); //n++ =2 
  return n;
}
alert(demo());

function A(){
	var t = 3;
	function _xx(){ alert(11+"****") ;}
	this.xx = function(){
		return _xx;
	}
}
A.prototype = {
	oth:function(){
		alert("普通方法");
	}
}
var a = new A();
var b = a.xx(); // a.xx()  ---> function _xx()
//b();
a.oth();
// 缺陷: 1 占用内存  2 不利于继承

继承

var person = function(){}
person.prototype.say = function(){
	alert("天气挺好");
}
var p = new person();
p.say(); // p 没有say方法的
// p.__proto__ = person.prototype  ---》 有say方法

var person = function(){};
person.prototype.say = function(){
	alert("天气挺好");
}
person.prototype.gongzi = 500;

var programmer = function(){};
programmer.prototype = new person();
programmer.prototype.wcd = function(){
	alert("明天天气也不错");
}
programmer.prototype.gongzi=1000;

var p = new programmer();
//p.say(); //可以调用
//p.wcd();
alert(p.gongzi);
// var p = new programmer();  p.__proto__ = programmer.prototype   = new person();
//var p1 = new person();   programmer.prototype = p1
// p.say(); p.__proto__ --> programmer.prototype ==p1 --->p1.__proto__  ==person.prototype.say();

function person(name,age){//父
	this.name= name;
	this.age = age;
}
person.prototype.sayhello = function(){
	alert("属性name值"+this.name);
}
/*
var per = new person("zhangsan",20);
per.sayhello();
*/
function student(){} ;//子
student.prototype = new person("李四",18);// 原型继承
student.prototype.grade = 3;
student.prototype.test = function(){
	alert(this.grade);
}
var  s = new student();
s.sayhello();
alert(s.grade);
//过程分析:
// s.__proto__ = student.prototype = p1    p1.__proto__ = person.protype.sayhello();

function parents(name){
	this.name = name;
	this.say = function(){
		alert("父亲的名字:"+this.name);
	}
}
function child(name,age){ //继承parents
	this.pObj = parents;//用父对象来创建子对象
	this.pObj(name);
	this.age = age;
	this.sayC = function(){
		alert("child:"+this.name+"---"+"age:"+this.age);
	}
}

var p = new parents("zhangsan");
p.say();
var c = new child("李四",20);
c.sayC();
//  李四---》 this.pObj(name); ---》 parents(name) ---> this.name=name="李四"
// this.sayC --->this.name--->parents-->this.name
//父对象 被子对象继承   所有的属性和方法,都将传递到子对象中*****

function person(name,age,len){
	this.name = name;
	this.age = age;
	this.len = len;
	this.say = function(){
		alert(this.name+":"+this.age+":"+this.len);
	}
}
//call继承
function student(name,age){
	person.call(this,name,age);
}
//apply继承
function teacher(name,age,len){
	person.apply(this,[name,age,len])
}

var per = new person("张三",25,"170");
per.say();
var stu = new student("李四",18);
stu.say(); // 李四 18 undefined
var tea = new teacher("王武",20,"180");
tea.say();

call apply

function add(a,b){
  alert(a+b);
}
function subs(a,b){
  alert(a-b);
}
add.call(subs,5,3); // subs-->add  ===>add(5,3)  subs只能引用一个存在的对象
add.apply(subs,[5,3]);

function animal() {
  this.name = "ani";
  this.showName = function () {
    alert(this.name);
  }
}

function cat() {
  this.name = "cat1";
}
var an = new animal();
var c = new cat();
an.showName.call(c, ","); // 通过call方法,将showName--》cat使用了
an.showName.apply(c, []);

arguments.callee

正在执行的function对象

var sum = function (n) {
  if (n <= 1) {
    return 1;
  } else {
    return n + arguments.callee(n - 1); //在函数内部调用本函数
  }
}
alert(sum(5));

arguments

function test(a, b, c) {
  alert(arguments.length);
  alert(arguments[1]); //arguments如果是表示参数,可以遍历的
}
test(1, 2, 3);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值