javascript中最常用的继承模式 组合继承
实现代码如下: //创建基类 function Person(name, age) { this.name = name; this.age = age; } //通过原型方式给基类添加函数(这样可以服用此函数) Person.prototype.showName = function () {
实现代码如下: //创建基类 function Person(name, age) { this.name = name; this.age = age; } //通过原型方式给基类添加函数(这样可以服用此函数) Person.prototype.showName = function () {
1、直接在前台调用 javascript 函数 很简单,在 head 元素之间加入 script 元素,将 type 元素设置为 " text/javascript " 如: 实现代码如下: function ShowName(str) { alert("您的名字为:("+str+")"); }
工厂模式: 初级开发者可能会这样定义对象: var obj = new Object(); obj.name = "hero"; obj.showName=function (){alert(this.name);} 这里存在一个问题就是如果我们要在多个地方用obj对象,可能在程序中类似的代码要写好
例子如下: 实现代码如下: /** *动物 */ function Animal(){ this.name='Amimal'; this.showName=function(){ alert(this.name); }; } /* *猫 */ function Cat(){ this.name='c