如何在Javascript中声明类? [英] How do I declare classes in Javascript?

查看:76
本文介绍了如何在Javascript中声明类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Javascript中创建类?说我有这个客户,但是我想从这个客户做另一个对象并更改名称,这样,我该怎么做?

How do I create a class in Javascript? Say I have this Customer, but I want to make another object from this Customer and change the name and such, how would I do this?

var Customer = {
        Name : "Frankie",
        PrintName : function(value)
        {
            alert(this.Name);
        }
    }

推荐答案

function Customer(name) {
    this.Name = name;
    this.PrintName = function () {
      alert(this.Name);  
    };
}

var customer1 = new Customer('Jake');
var customer2 = new Customer('Mike');
customer1.PrintName(); 
customer2.PrintName(); 

这篇关于如何在Javascript中声明类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆