User.prototype.sayHello=function(){
alert("hello");
}
user.sayHello();这样的话弹出hello
但是我换一种方式
var user=new User();User.prototype={
sayHello:function(){
alert("hello");
}
}user.sayHello();ff报user.sayHello is not a functionjavascriptprototypejs原型
alert("hello");
}
user.sayHello();这样的话弹出hello
但是我换一种方式
var user=new User();User.prototype={
sayHello:function(){
alert("hello");
}
}user.sayHello();ff报user.sayHello is not a functionjavascriptprototypejs原型
//code here
}
var user=new User();
User.prototype={
sayHello:function(){
alert("hello");
}
}报错user.sayHello is not a function
function User(){
//code here
}
var user=new User();User.prototype.sayHello=function(){
alert("hello");
}user.sayHello();弹出hello
原型的绑定不能晚于对象的创建,也就是说要在new对象前。
User.prototype.sayHello = function(){}
给原型添加一个方法后,该构造函数的实例都会有。
function User(){
//code here
}
假设此时User的原型(User.prototype)是User_proto。那么当
var user=new User()
user的原型是User_proto。
此时User.prototype.sayHello=function(){
alert("hello");
}
只是在User_proto上添加了一个sayHello方法。user.sayHello可用。
但是,当User.prototype={
sayHello:function(){
alert("hello");
}
}把User类的原型换成。可是user的原型并没有换。还是老的User_proto。所以user.sayHello不可用
User.prototype.sayHello=function(){
alert("hello");
}
这个算是什么呢,不叫原型绑定么
function User(){
//code here
}
var methods={
sayHello:function(){
alert("hello");
}
}
User.prototype=methods;
methods={
sayHello:function(){
alert("hello");
},
sayWord:function(){
alert("word");
}
}
var user=new User();
user.sayWord();也是user.sayWord is not a function
之前的懂了,谢谢,现在又有个问题function User(){
//code here
}
var methods={
sayHello:function(){
alert("hello");
}
}
User.prototype=methods;
methods={
sayHello:function(){
alert("hello");
},
sayWord:function(){
alert("word");
}
}
这样不可以
//code here
}
var methods={
sayHello:function(){
alert("hello");
}
}
methods={
sayHello:function(){
alert("hello");
},
sayWord:function(){
alert("word");
}
}
User.prototype=methods;
var user=new User();
user.sayWord();
好像不可以啊
function User(){
//code here
}
var methods={
sayHello:function(){
alert("hello");
}
}
User.prototype=methods;
methods={
sayHello:function(){
alert("hello");
},
sayWord:function(){
alert("word");
}
}var user=new User();
user.sayWord();
//code here
}
User.prototype={
sayHello:function(){
alert("hello");
},
sayWord:function(){
alert("word");
}
}
var user=new User();
user.sayWord();function User(){
//code here
}
User.prototype={
sayHello:function(){
alert("hello");
}
}
var user=new User();
/*给实例添加一个sayWord方法*/
User.prototype.sayWord = function(){
alert("word");
}
user.sayWord();
User.prototype.sayHello=function(){
alert("hello");
}
你写的这种写法就可以满足你的需求嘛。
另外,觉得楼主的基础知识很不熟。
给你推荐一篇文章看看。能领悟多少看你自己了
http://www.cnblogs.com/RicCC/archive/2008/02/15/JavaScript-Object-Model-Execution-Model.html
//code here
}
var methods={
sayHello:function(){
alert("hello");
}
}
User.prototype=methods;
methods.sayWord:function(){
alert("word");
}var user=new User();
user.sayWord();
//code here
}
var methods={
sayHello:function(){
alert("hello");
}
}
User.prototype=methods;
methods.sayWord=function(){
alert("word");
}var user=new User();
user.sayWord();
sayHello:"hello"
}
var test=methods;
methods={
sayWord:"word"
}
alert(test.sayWord);//undefined因为对象时引用类型,个人认为之后修改的是methods堆得值,引用未改变所以个人认为解释不通啊