欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javascript 中类的定义

程序员文章站 2022-04-04 16:05:10
...
//声明方法一
function person(name,age){
    
this.name=name;
    
this.age=age;
    };
person.prototype.show
=function(){
       alert(
"您输入的姓名是:"+this.name+",您的年龄是:"+this.age);
    
    };
//声明方法二
    function person(name,age)
    {
        
this.name=name;
        
this.age=age;
        
this.show=function(){
               alert(
"您输入的姓名是:"+this.name+",您的年龄是:"+this.age);
            };
        
        }

//调用   
        var p=new person("wangk","19");
        p.show();
 
String.prototype.format = function () {
var args = arguments;
return this.replace(/\{(\d+)\}/g,
function (m, i) { return args[i]; }
);
}
String.prototype.endsWith = function (suffix) {
return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
String.prototype.startsWith = function (suffix) {
return this.indexOf(suffix) == 0;
};
String.prototype.trim = function () {

return !this
? this
: this.replace(/^( |\s)*|( |\s)*$/gi, '');
};
String.prototype.filter = function () {
return !this
? this
: this.replace(/[^0-9a-zA-Z\u4e00-\u9fa5\s]/ig, ""); 
}

 

var Order = function (_oid, _state, settingOptions) {
var _self = this;

this.Init = function () {

}
var oid = _oid;
var state = _state;
this.Price = 0;

this.ShowOptions = function () {

// alert(_self.settings.isOnline);
};

this.Show = function () {
alert("Show:" + oid);
};


this.Alert = function () {
return priveatMethod();
};


var priveatMethod = function () {
alert("Price:" + _self.Price);
};

};

 

var ord = new Order("1", "rhythmk", {
isOnline:"_isonline"
});
ord.Init();
ord.Show();
ord.Alert();
ord.ShowOptions();

 

 // 类定义

     // 静态类
    var adm = {
        xOffset: 10,
        yOffset: 20,
        
        GetData: function() {
            alert(this.xOffset + "," + this.yOffset);
        },
        
        ModifyData: function(x, y) {
            this.xOffset = x;
             this.yOffset = y;
        }
    };

    adm.GetData();
    adm.ModifyData(100, 200);
    adm.GetData();
    adm.xOffset = 10000;
    adm.GetData();

   alert("----JS 类---")

   var show = function() {
        // 私有对象
        _seft = this;
        // 私有对象
        var xOff = 10;
        var yOff = 20;
        this.PublicData = 0;
        
        // 公有方法
        this.Alert = function() {
            alert(xOff + "," + yOff + "," + _seft.PublicData);
        };
        
        // 私有方法
        var PriMethod = function() {
            alert("私有方法");
        };
        // 公有方法
        this.CallPMethod = function() {
            return PriMethod();
        };

    }


    var s = new show();
    s.Alert();
    s.CallPMethod();
    s.PublicData = 66666;
    s.Alert();
   
 jquery 插件开发:
	// 定义一个JS 插件
	;(function($){
	   $.fn.man=function(options)
	   {
	         var defaults={
			    name:"rhythmk",
				age:20,
				url:"rhythmk.cnblogs.com"
			};
			 
			options= $.extend(defaults,options);
			this.show=function(){
			   alert("name:"+options.name+",age:"+options.age+",email:"+options.url);
			};
		};
	})(jQuery);
	
	
	//插件调用
	var data={name:"wkaaaaaaa"};
	
	var  man=$.fn.man;
	
	//直接使用插件
	var m=new $.fn.man(data);
	m.show();
	
	//插件别名
	var  man=$.fn.man;
	var m1=new man();
	m1.show();

  

 
 
 
 

转载于:https://www.cnblogs.com/rhythmK/archive/2009/08/18/1548920.html