日期:2014-05-16  浏览次数:20359 次

javascript类与对象结合理解jsUI框架
以下代码都是网上找的,便于理解与方便查找学习之用!!

构造函数方式

function Car(sColor,iDoors,iMpg){

this.color = sColor;

this.doors = iDoors;

this.mpg = iMpg;

this.showColor = function(){

alert(this.color);

}

}

var Car1 = new Car("red",4,23);

Car1.showColor();

原型方式+构造方法

function Car(name){

this.name = name;

}

Car.prototype.color ="blue";

Car.prototype.doors = 4;

Car.prototype.mpg = 25;

Car.prototype.showColor = function(){

alert(this.color);

}

var Car1 = new Car();

car1.showColor();

function Person(name){

this.name = name;

}

Person.prototype={

printName:function(){

alert(this.name);

}

}

var person = new Person("kk");

person.printName();

动态原型方式----完美解决方案

function Car(sColor){

this.color = sColor;

if(Car.prototype.say =="undefined"){

Car.prototype.say = function(){

alert(this.color);

}

}

}

var Car1 = new Car("111");

Car1.say();

继承的实现方式

1、对象冒充

function Car(name){

this.name = name;

this.say = function(){

alert(this.name);

}

}

function bigCar(name){

this.inherit = Car;

this.inherit(name);

delete this.inherit;

this.color = function(){

alert("I am bigCar");

}

}

var car = new bigCar("futian");

car.say();

2、利用call()和apply()冒充

function Person(name,age){

this.name = name;

this.age = age;

this.say = function(){

alert(this.name);

}

}

function WhitePeople(name,age){

People.call(this,name,age);//call方式以多个参数进行传值

People.apply(this,[name,age]);//apply方式以数组方式进行传值

this.color=function(){

alert("i am");

}

}

var p = new WhitePeople("wang",34);

p.say();

p.color();

JS 实现 创建类、继承、方法添加、对象克隆、数组封装操作

一.对象的创建以及方法的添加





/** 1.类创建,可继承

* Parent,父类

*newparams,当前新创建类的参数集合,包括属性和方法

*/

window.Class = {

    Create: function (parent,newparams) {

        var NewFun=function () {

            if (typeof this.initialize != 'undefined') {

                this.initialize.apply(this, arguments);

            }

        }

        if(parent!=null){

            if (typeof parent == 'function') {

                NewFun.prototype = new parent(Array.prototype.slice.apply(arguments, [1]));

                NewFun.prototype.constructor = NewFun;

                NewFun._parent = parent;

                /*                 * 实现调用父类函数的方法

   &