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())

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章