最简单的创建对象的两种方式
// (1)
var person=new Object();person.name="jxj";person.age="24";person.sayHi=function (){ console.log("我的名字叫" + this.name + ",今年" + this.age + "岁");} // (2) var person={ name:"jxj", age:"24", sayHi:function(){ console.log("我的名字叫" + this.name + ",今年" + this.age + "岁"); }}
工厂模式
function createPerson(name, age) {var o = new Object();o.name = name;o.age = age;o.sayHi = function () { console.log("我的名字叫" + this.name + ",今年" + this.age + "岁");};}var p1 = new createPerson("jxj", "24");var p1 = new createPerson("jxj", "25");
//根据接受的参数构建的对象
问题,对象识别----返回的对象都是Object类型的(如果createCar(), , , car1, car2, p1, p2都是ojbect类型的)
typeof p1; //返回“object”;p1 instanceof Object //返回 trueObject.prototype.toString.call(p1); //返回"[object Object]"
构造函数
function Person(name, age) {this.name = name;this.age = age;this.sayHi = function () {console.log("我的名字叫" + this.name + ",今年" + this.age + "岁")};this.sayBye = function () {console.log("sayBye")};}var jxj = new Person("jxj", "24");var jxj2 = new Person("jxj2", "25");
//成功解决对象识别问题jxj instanceof Person //返回truejxj instanceof Object //返回true,所有对象均继承自Object//问题:/每个队形独有一份属性和方法,但实际在运用中方法,一个方法完成同一个功能,没有必要实例单独有一份(每个方法都是一个对象);function Person(name, age) {this.name = name;this.age = age;this.sayHi = sayHi;this.sayBye = sayBye;}function sayHi() {console.log("我的名字叫" + this.name + ",今年" + this.age + "岁")};function sayBye() {console.log("sayBye")};
//问题:
//挂载全局作用域中,定义多个方法会定义多个全局函数
//没有封装性
//拓展:构造函数与函数的区别:调用方式
//当做普通函数使用
Person("jxj", "24");//this指向全局window,属性添加到window对象上window.name;window.sayHi();
js原型模式
js原型对象的知识(Person Prototype原型对象,prototype属性,[Prototype],constructor)
Js中创建一个函数(包括普通的函数,及构造函数,函数表达式),会创建一个prototype属性,指向函数的原型对象;
每一个对象(原型对象的指针指向object prototype原型对象)都用一个[Prototype]指针,指向原型对象
Person.prototype.isPrototypeOf(jxj);//返回true Object.getPrototypeOf(jxj)==Person.prototype;//返回true function hehe(){}Object.getPrototypeOf(hehe.prototype);//Object {}var a=new hehe();hehe.prototype.isPrototypeOf(a);//trueObject.prototype.isPrototypeOf(hehe.prototype);//true
创建构造函数后,其原型对象默认取得constructor属性,指向构造函数(Person),(对象调用constructor均出自其原型对象)
function Person(name) {this.name = name;}var p1 = new Person("jxj");p1.constructorp1.hasOwnProperty("constructor") false Person.prototype.hasOwnProperty("constructor") Truep1.constructor === Person.prototype.constructor true Person.prototype.isPrototypeOf(p1);true Object.getPrototypeOf(Person)function Empty() {} Function.prototype.isPrototypeOf(Person) true Object.getPrototypeOf(p1) Person {} Object.getPrototypeOf(Person) function Empty() {} (1)Person.prototype,挂载属性和方法function Person() {}Person.prototype.name = "jxj" ;Person.prototype.age = "24" ;Person.prototype.sayHi = function () {console.log("我的名字叫" + this.name + ",今年" + this.age + "岁")};var jxj = new Person();var jxj2 = new Person();
(2)对象字面量,重写整个原型对象
function Person() {} Person.prototype = { //constructor:Person name : "jxj" , age : "24" , //friend :{"aaa","bbb","ccc"},sayHi : function () {console.log("我的名字叫" + this.name + ",今年" + this.age + "岁")};}var jxj = new Person(); var jxj2 = new Person(); jxj instanceof Person;//true;jxj.constructor==Person;//false;实际上constructor已经指向Object构造函数//所有实例都共享了一份属性jxj.friend.push("ddd");jxj.friend===jxj2.friend;//返回true,{"aaa","bbb","ccc","ddd"};组合使用构造函数模式和原型模式(最为常用的模式)function Person(name, age) {this.name = name;this.age = age;friend :{"aaa","bbb","ccc"};this.sayHi = function () {console.log("我的名字叫" + this.name + ",今年" + this.age + "岁")};
Person.prototype={constructor:Person,sayHi:function () { console.log("我的名字叫" + this.name + ",今年" + this.age + "岁")},this.sayBye = function () {console.log("sayBye") }}
//构造函数模式,用于定义实例的属性;
//原型模式用于定义,方法,以及想要共享的属性
继承---(原理利用原型链,让原型对象等于另一个类型的实例)
//父function Father (){ this.fatherHeight="1.75";}Father.prototype.getFatherHeight=function(){console.log(this.fatherHeight);}//子function Child(){ this.childHeight="1.20";}//子类继承父类Child.prototype=new Father();Child.prototype.getHeight=function (){console.log(this.childHeight);} var c=new Child(); c.getFatherHeight();