创建类传递参数无效,求修改

yld2017 2017-02-15 10:45:40



var Cat = {
    name: "大毛",
    makeSound: function(){ alert(name); }
  };

var c = new Cat();
alert(c.name)
c.makeSound();

...全文
327 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuozhu01 2017-07-15
  • 打赏
  • 举报
回复

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
/*
$=function(b){
	return document.getElementById(b)
};
*/

//深度拷贝
_=function(){
	var b,a,f,
		e=arguments[0],
		d=arguments.length;
	alert(d);
	for(var c=0;c<d;c++)
	{
		b=arguments[c];
		for(a in b)
		{
			alert("类型=="+typeof b[a]+"【】"+b[a]);
			e[a]= typeof b[a] ==='object'? deepCopy(b[a]): b[a];
		}
	}
	return e
};
	
/*	
_$=function(){
	var b,a,
		e=arguments[0],
		d=arguments.length;
	for(var c=1;c<d;c++)
	{
		b=arguments[c];
		for(a in e)
		{
			e[a]===undefined||(e[a]=b[a])
		}
	}
	return e;
};
*/
</script>
</head>

<body>

<script type="text/javascript">
var Cat = {
  name: "大毛",
  makeSound: function(){ alert(this.name); }
};
  
var c = _(Cat);
alert("c.name=="+c.name);
c.makeSound();

d=_(Cat,{name:'二毛'});
alert("d.name=="+d.name);
d.makeSound();

</script>
</body>
</html>

yld2017 2017-07-15
  • 打赏
  • 举报
回复

var Cat = {
  name: "大毛",
  makeSound: function(){ alert(this.name); }
};
  


if(a){
	var a=a;     
}
else{
	var a={}    //将一个空对象传给a
}
var deepCopy= function(source) {
	var result={};
	for (var key in source) {
		alert("类型=="+typeof source[key]+"【】"+source[key]);
		result[key] = typeof source[key]==='object'? deepCopy(source[key]): source[key];
	} 
	return result; 
}

var person={name:'chen',age:18,man:{hight:188}}
var son = deepCopy(person);
son.man.hight=1999;
alert(son.man.hight);
alert(person.man.hight);
yld2017 2017-07-15
  • 打赏
  • 举报
回复
引用 1 楼 Free_Wind22 的回复:

var Cat = function (){
    this.name =  "大毛",
    this.makeSound = function(){ alert(this.name); }
  };
 
var c = new Cat();
alert(c.name)
c.makeSound();
非常感谢您的指点
yld2017 2017-03-21
  • 打赏
  • 举报
回复


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
	var $=function(b){return document.getElementById(b)}
</script>
</head>

<body>
<script>
var Cat = {
  name: "大毛",
  makeSound: function(){ alert(this.name); }
};
var c = Cat;
alert("c.name=="+c.name);
c.makeSound();

d={
	name:'二毛',
	__proto__:Cat,
};
alert("d.name=="+d.name);
d.makeSound();

</script>

</body>
</html>

leeqihero123 2017-03-02
  • 打赏
  • 举报
回复
<html> <head> <meta charset="utf-8"> <script> $=function(b){return document.getElementById(b)} </script> </head> <body> <canvas id=0></canvas> </body> <script> var Cat = {   name: "大毛",   makeSound: function(){ alert(this.name); } }; var c = Cat; alert(c.name); c.makeSound(); d={ name:'二毛', __proto__:Cat, }; alert(d.name); d.makeSound(); </script> </html>
蚂蚁上树 2017-03-02
  • 打赏
  • 举报
回复
引用 3 楼 Free_Wind22 的回复:
lev("打打") => return lev("打打"); {}这种创建的直接就是对象了,不要用new

var Cat = {
    name: "大毛",
    makeSound: function(){ alert(this.name); }
  };
 
alert(Cat.name)
Cat.makeSound();
+1
leeqihero123 2017-03-01
  • 打赏
  • 举报
回复
<html>
<head>
<meta charset="utf-8">
<script>
$=function(b){return document.getElementById(b)};_=function(){var b,a,f,e=arguments[0],d=arguments.length;for(var c=1;c<d;c++){b=arguments[c];for(a in b){e[a]=b[a]}}return e};_$=function(){var b,a,e=arguments[0],d=arguments.length;for(var c=1;c<d;c++){b=arguments[c];for(a in e){e[a]===undefined||(e[a]=b[a])}}return e};
</script>
</head>
<body>
<canvas id=0></canvas>
</body>
<script>
var Cat = {
  name: "大毛",
  makeSound: function(){ alert(this.name); }
};
 
var c = _(Cat);
alert(c.name);
c.makeSound();
d=_(Cat,{name:'二毛'});
alert(d.name);
d.makeSound();

</script>
</html>
离子漂浮物 2017-02-17
  • 打赏
  • 举报
回复
楼主说的那个带冒号的{},严格来说是个集合对象。 既然是个对象,那你对一个对象new也new不出什么东西来的呢。
2017-02-15
  • 打赏
  • 举报
回复
lev("打打") => return lev("打打"); {}这种创建的直接就是对象了,不要用new

var Cat = {
    name: "大毛",
    makeSound: function(){ alert(this.name); }
  };
 
alert(Cat.name)
Cat.makeSound();
yld2017 2017-02-15
  • 打赏
  • 举报
回复
引用 1 楼 Free_Wind22 的回复:

var Cat = function (){
    this.name =  "大毛",
    this.makeSound = function(){ alert(this.name); }
  };
 
var c = new Cat();
alert(c.name)
c.makeSound();


var lev=function(a){
   return a;
};  
function Parent(){  
 
	this.name="李小龙";  
	this.age="30";  
	this.lev123=function(){
		lev("打打")
	};  
};  
  
var  x =  new Parent();
//alert(x.name);  
alert(x.lev123());
问下为什么这个无效


var Cat = {
    name: "大毛",
    makeSound: function(){ alert(name); }
  };
 

这种带冒号的为什么不行?
2017-02-15
  • 打赏
  • 举报
回复

var Cat = function (){
    this.name =  "大毛",
    this.makeSound = function(){ alert(this.name); }
  };
 
var c = new Cat();
alert(c.name)
c.makeSound();

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧