JS做成像程序中的类一样

JS做成像程序中的类一样


如我有
Tools.js文件
里面有一个
function MessageShow(message)
{
    Alert(message);
}
function Show(message)
{
    Alert(message);
}

现在如果把文件引入网页的话调用是:
MessageShow("测试");

但现在我想要用
Tools.MessageShow("类形式调用");

这种方式,我的文件要怎么改动

谢谢

 

var Tools = {
MessageShow:function(message){alert(message)},
Show:function(message){alert(message)}
}

 

 

方式一:function way// First, define some functions that will be used as methods.

function Rectangle_area(  ) { return this.width * this.height; }

function Rectangle_perimeter(  ) { return 2*this.width + 2*this.height; }

function Rectangle_set_size(w,h) { this.width = w; this.height = h; }

function Rectangle_enlarge(  ) { this.width *= 2; this.height *= 2; }

function Rectangle_shrink(  ) { this.width /= 2; this.height /= 2; }

 

// Then define a constructor method for our Rectangle objects.

// The constructor initializes properties and also assigns methods.

function Rectangle(w, h)

{

    // Initialize object properties.

    this.width = w;

    this.height = h;

 

    // Define methods for the object.

    this.area = Rectangle_area;

    this.perimeter = Rectangle_perimeter;

    this.set_size = Rectangle_set_size;

    this.enlarge = Rectangle_enlarge;

    this.shrink = Rectangle_shrink;

}

// Now, when we create a rectangle, we can immediately invoke methods on it:

var r = new Rectangle(2,2);

alert(r.area());

alert(r.enlarge());

alert(r.perimeter());  方式二:prototype way(better)prototype way is better than function way!
because it takes up less memory space.//solution 2:
//this solution is better than solution 1
//use object's prototype
// Define a constructor method for our class.
// Use it to initialize properties that will be different for
// each individual Circle object.
function Circle(x, y, r)
{
    this.x = x;  // The X-coordinate of the center of the circle
    this.y = y;  // The Y-coordinate of the center of the circle
    this.r = r;  // The radius of the circle
}
 
// Define a constant: a property that will be shared by
// all circle objects. Actually, we could just use Math.PI,
// but we do it this way for the sake of instruction.
Circle.prototype.pi = 3.14159;
 
// Define a method to compute the circumference of the circle.
// First declare a function, then assign it to a prototype property.
// Note the use of the constant defined above.
function Circle_circumference(  ) { return 2 * this.pi * this.r; }
Circle.prototype.circumference = Circle_circumference;
 
// Define another method. This time we use a function literal to define
// the function and assign it to a prototype property all in one step.
Circle.prototype.area = function(  ) { return this.pi * this.r * this.r; }
 
// The Circle class is defined.
// Now we can create an instance and invoke its methods.
var c = new Circle(0.0, 0.0, 3);//radius=2
alert(c.area());
alert(c.circumference());
 
/*
*Notes:
*It is not only user-defined classes that have prototype objects.
*Built-in classes, such as String and Date,
*have prototype objects too, and you can assign values to them.
*For example, the following code defines a new method that is available for all String objects:
*/ String.prototype.Trim = function()
{
    return this.replace(/(^/s*)|(/s*$)/g, "");
}
String.prototype.LTrim = function()
{
    return this.replace(/(^/s*)/g, "");
}String.prototype.RTrim = function()
{
    return this.replace(/(/s*$)/g, "");
}alert("A ".Trim()+" B".Trim())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值