javascript 中的继承方法
1.可以通过prototype属性,实现继承方法的方式,这种方式就是java语言中继承的变换形式。
// Create the constructor for a Person object
function Person( name ) {
this.name = name;
}
// Add a new method to the Person object
Person.prototype.getName = function() {
return this.name;
};
Person.prototype.setName = function(name) {
this.name = name;
};
// Create a new User object constructor
function User( name, password ) {
// Notice that this does not support graceful overloading/inheritance
// e.g. being able to call the super class constructor
this.name = name;
this.password = password;
};
// The User object inherits all of the Person object's methods
User.prototype = new Person();
2.通过自己写继承函数,实现继承,下面这段代码也很好,值得一看
// A simple helper that allows you to bind new functions to the
// prototype of an object
Function.prototype.method = function(name, func) {
this.prototype[name] = func;
return this;
};
// A (rather complex) function that allows you to gracefully inherit
// functions from other objects and be able to still call the 'parent'
// object's function
Function.method('inherits', function(parent) {
// Keep track of how many parent-levels deep we are
var depth = 0;
// Inherit the parent's methods
var proto = this.prototype = new parent();
// Create a new 'priveledged' function called 'uber', that when called
// executes any function that has been written over in the inheritance
this.method('uber', function uber(name) {
var func; // The function to be execute
var ret; // The return value of the function
&n
相关文档:
在js中,每个对象都有一个prototype属性:返回对象类型原型的引用。很拗口!习语“依葫芦画瓢”,这里的葫芦就是原型,那么“瓢.prototype” 返回的就是葫芦,或者“瓢.prototype= new 葫芦()”。
prototype的用途:
继承
有一个对象--子类:
function 子类() {
this.lastname = ......
语法
oNewWindow = window.open( [sURL] [, sName] [, sFeatures] )
sURL 可选. URL 字符串 . 如果URL为空, 将以about:blank打开.
sName 可选. 字符串 描述打开窗口的名字(name). 可以做为form 和 a 标签的TARGET属性值 .
sFeatures 可选. 字符串 格式如"fullscreen=yes,toolbar=yes".channelmode = { yes | no | ......
一、类型转换的方法和应该注意的问题:
1,转换为布尔型:
(1)用两次非运算(!):
!!5 ==> true
(2)用布尔型的构造函数:
new Boolean(5) == > true
值转换为布尔类型为false:
0,+0,-0,NaN,""(空字符串),undefined,null
除上面的值其他值在转换以后为true,需要特别提到的是:
"0",new Object(),funct ......
一个简单的javascript类定义例子
涵盖了javascript公有成员定义、私有成员定义、特权方法定义的简单示例
Java代码
<script>
//定义一个javascript类
function JsClass(privateParam/*&n ......
javascript 类定义4种方法
Java代码
/*
工厂方式--- 创建并返回特定类型的对象的 工厂函数 ( factory function )
*/
function createCar(color,doors,mpg){
......