面向对象分为 基于原型的面向对象和基于模板的面向对象。
JavaScript:面向对象,基于事件的网页脚本语言。
Java:基于模板的面向对象。
class A
{
private String name;
public void fun()
{
}
}
A a = new A();
a.fun();
js:基于原型的面向对象。
function fun()
{
var user = new Object();
user.id = 1;
user.name='zhangsan';
user.sex = 'Male';
user.sayHello=fun2();
}
function fun2()
{
alert('Hello');
}
1.基于原型的创建对象:
var obj = new Object();
obj.id = 1;
obj.name='zhangsan';
obj.sayHello = fun2;
obj.sayHello = function(){
fdafdsafds
fdsafafa
fdsafsa
};
2.基于JS对象的数组表示方法:
var obj = {
id:1,
name:'zhangsan',
sayHello:function(str){
alert(str);
}
};
var user = eval(obj);
alert(user.name);
user.sayHello('dfdafa');
3.利用函数来创建JS对象:
function User(id,name,sex)
{
this.id = id;
this.name = name;
this.sex = sex;
this.sayHello = function(){
alert('fddfdsa');
}
}
var user = new User(1,'zhangsan','Male');
user.id
user.name
user.sayHello();
var user = new Object();
user.id = 1;
user.name='zhangsan';
user.sex = 'Male';
user.sayHello=fun2();
}
function fun2()
{
alert('Hello');
}
1.基于原型的创建对象:
var obj = new Object();
obj.id = 1;
obj.name='zhangsan';
obj.sayHello = fun2;
obj.sayHello = function(){
fdafdsafds
fdsafafa
fdsafsa
};
2.基于JS对象的数组表示方法:
var obj = {
id:1,
name:'zhangsan',
sayHello:function(str){
alert(str);
}
};
var user = eval(obj);
alert(user.name);
user.sayHello('dfdafa');
3.利用函数来创建JS对象:
function User(id,name,sex)
{
this.id = id;
this.name = name;
this.sex = sex;
this.sayHello = function(){
alert('fddfdsa');
}
}
var user = new User(1,'zhangsan','Male');
user.id
user.name
user.sayHello();