在javascript中也可以创建类似c#的类,可以添加prototype Virus

我们可以在c#定义下面这样的类,其实在javascript也可以做类似的事情,在c#中定义类,代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Album 的摘要说明
/// </summary>

public class Album
{
    
private string _title = string.Empty;
    
private string _artist = string.Empty;

    
public Album(string title,string artist)
    
{
        
this._title = title;
        
this._artist = artist;
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }

}

javascript代码如下:

<script type="text/javascript">
       
        
        
function Album(title,artist)
        
{
            
this._title=title;
            
this._artist=artist;
        }

        
var album1=new Album("love","swb");
        
var album2=new Album("love too","zy");
        
        
function listing()
        
{
            
return this._artist+":"+this._title;
        }

        Album.prototype.Listing
=listing;
        
        alert(album1.Listing());
        alert(album2.Listing());
    
</script>

【Blog】http://virusswb.cnblogs.com/

【MSN】jorden008@hotmail.com

【说明】转载请标明出处,谢谢

反馈文章质量,你可以通过快速通道评论:

原文地址:https://www.cnblogs.com/virusswb/p/1118565.html