[JavaScript]类之二
javascript 类定义4种方法
Java代码
/*
工厂方式--- 创建并返回特定类型的对象的 工厂函数 ( factory function )
*/
function createCar(color,doors,mpg){
var tempCar = new Object;
tempCar.color = color;
tempCar.doors = doors;
tempCar.mpg = mpg;
tempCar.showCar = function(){
alert(this.color + " " + this.doors);
}
return tempCar;
}
/*
构造函数方式--- 构造函数看起来很像工厂函数
*/
function Car(color,doors,mpg){
this.color = color;
this.doors = doors;
this.mpg = mpg;
this.showCar = function(){
alert(this.color);
};
}
/*
原型方式--- 利用了对象的 prototype 属性,可把它看成创建新对象所依赖的原型
*/
function Car(color,doors,mpg){
this.color = color;
this.doors = doors;
this.mpg = mpg;
this.drivers = new Array("nomad","angel");
} &nb
相关文档:
1、对象的继承,一般的做法是复制:Object.extend
prototype.js的实现方式是:
Object.extend = function(destination, source){
for (property in source) {
destination[property] = source[property];
}
return destination;
......
在js中,每个对象都有一个prototype属性:返回对象类型原型的引用。很拗口!习语“依葫芦画瓢”,这里的葫芦就是原型,那么“瓢.prototype” 返回的就是葫芦,或者“瓢.prototype= new 葫芦()”。
prototype的用途:
继承
有一个对象--子类:
function 子类() {
this.lastname = ......
定义与用法
The prototype property allows you to add properties and methods to an
object.
prototype属性允许你向一个对象添加属性和方法
Syntax
语法
object.prototype.name=value
Example 1
实例
In this example we will show how to use the prototype property to add a
property to an object:
在下 ......
二、动态给表插入行:
function addRow(){
//动态插入一行
var oRow1=mediaMes.insertRow(mediaMes.rows.length);
//设置tr的id
oRow1.id="tr"+thisId;
//获得表总的行数
var aRows=mediaMes.rows;
//获得新添加行的列集合
var aCells=oRow1. ......
思路:浏览图片,读取其宽度ImageW,高度值ImageH。读取用户输入的分成几行Row几列Col。
每个方块:boxW=ImageW/Col;boxH=ImageH/Row;
在1到Row*Col中随机,第i个图片的位置设为pos【i】。然后动态创建div生成Row* ......