Rumah > Artikel > hujung hadapan web > Javascript 创建类并动态添加属性及方法的简单实现
JavaScript 是一种很强的面向对象的语言,支持创建实例之后再添加属性和方法,虽然是小技巧,用的时候容易忘记,今天写了一个很小的例子,记录在这里,仅供参考。
function MyClass() { //This function is same as a constructer alert("New Object Created"); } //Creating Object var MyObject = new MyClass (); NewObject.prototype = { //Adding Method named "MyMethod" MyMethod: function(){alert("My Method");} , //Adding property named "MyProperty" MyProperty: "My Property" } //Calling Method MyObject.MyMethod(); //Assigning Property MyObject.MyProperty = "My Property Value changed";