javascript中function和object的区别,以及javascript如何实现面向对象的编程思想.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
function aa() {
window.alert("aa");
}
var bb = function () {
window.alert("bb");
}
var cc = new Function("window.alert('cc');");
window.alert(typeof cc);
</script>
</head>
<body> </body>
</html>
fun01
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
function fn1() {
window.alert("fn1");
} //fn1();
var fn2 = fn1; fn2(); fn1 = function () {
window.alert("new fn1");
}; fn2(); fn1(); </script>
</head>
<body> </body>
</html>
fun02
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
function sum1(a, b) {
return a + b;
}
function sum1(a) {
return a + a;
}
//window.alert(sum1(4, 5));
window.alert(sum1(4,5)); </script>
</head>
<body> </body>
</html>
fun03
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>function 参数</title>
<script language="javascript" type="text/javascript">
function callFun(fun, arg) {
return fun(arg);
} function say(name) {
window.alert(name);
} //say("wyp");
callFun(say, "wyp"); function say(name) {
window.alert("new " + name);
}
callFun(say, "wyp"); var cc = new Function("name", "say(name)");
cc("wangyp"); var ss = [1, 2, 11, 13, 12, 119];
window.alert(ss);
ss.sort(sortBy);
window.alert(ss); function sortBy(a, b) {
return a - b;
}
</script>
</head>
<body> </body>
</html>
fun04
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>function 返回值</title>
<script language="javascript" type="text/javascript">
//function fn1(a) {
// var fnn1 = function (b) {
// return a + b;
// }
// return fnn1;
//}
//var fn11 = fn1(3);
//window.alert(fn11(4)); function compareProp(prop) {
var fn1 = function (obj1, obj2) {
if (obj1[prop] > obj2[prop]) return 1;
else if (obj1[prop] < obj2[prop]) return -1;
return 0;
}
return fn1;
} var person1 = { name: 'wyp', age: 33 };
var person2 = { name: 'zyx', age: 23 };
var person3 = { name: 'hg', age: 27 };
var persons = [person1, person2, person3];
//for (var i = 0 ; i < persons.length; i++) {
// window.alert(persons[i].name + "," + persons[i].age);
//}
var comparePropFun = compareProp("name");
persons.sort(comparePropFun);
for (var i = 0 ; i < persons.length; i++) {
window.alert(persons[i].name + "," + persons[i].age);
}
</script>
</head>
<body> </body>
</html>
fun05
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
//function fn() {
// window.alert(arguments.length);
// var result = 0;
// for (var i = 0 ; i < arguments.length ; i++) {
// result += arguments[i];
// }
// return result;
//} //window.alert(fn(1, 3, 5)); function sum(num) {
if (num == 1) {
return 1;
}
else {
return num * arguments.callee(num - 1);
//return num * sum(num - 1);
}
}
//window.alert(sum(3)); var fn = sum;
sum = null;
window.alert(fn(3)); </script>
</head>
<body> </body>
</html>
fun06
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
function Person(name, age) {
this.name = name;
this.age = age;
}
window.alert(typeof Person);
var person = new Person("wyp",33);
//person.name = "wyp";
window.alert(typeof person);
window.alert(person.name); //function Person(name, age) {
// window.alert(arguments.length);
//} //window.alert(Person.length);
//Person(10);
</script>
</head>
<body> </body>
</html>
fun07
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
var person1 = { name: "wyp", age: 33 };
var person2 = { name: "cr", age: 29 }; function show(a, b) {
window.alert("name=" + this.name + ",a=" + a + ",b=" + b);
}
show(3, 4);
show.apply(person1, [3, 4]);
show.call(person2, 3, 4);
</script>
</head>
<body> </body>
</html>
fun08
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
var person = '{ name: "wyp", age: 32 }';
//var obj = eval("(" + person + ")");
var obj = new Function("return " + person)();
window.alert(obj.name);
</script>
</head>
<body> </body>
</html>
json1
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
//var aa = new Array();
//var aa = new Object();
var aa = { }; aa[0] = "wyp";
aa[1] = "wangyunpeng";
aa.name = "shuaige";
//aa["name"];
//aa.name;
window.alert(aa[1]);
</script>
</head>
<body> </body>
</html>
obj01
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
var person = { name: "wyp", age: 32 };
person.sex = true;
window.alert(person.name); </script>
</head>
<body> </body>
</html>
obj02
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
//var obj1 = { name: 'wyp' };
//var obj2 = obj1;
//window.alert(obj2.name);
//obj1.name = "wangyunpeng";
//window.alert(obj2.name); var obj = { name: "ddd" }; </script>
</head>
<body> </body>
</html>
obj03
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
//var person = new Object();
//person.name = 'wyp';
//person.age = 33;
//person.say = function () {
// window.alert(this.name);
//}
//person.say(); var person = {
name: "wyp",
age: 33,
say: function () {
window.alert(this.name);
}
}
person.say();
</script>
</head>
<body> </body>
</html>
obj04
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
var Person = function () { }; var person = new Person();
window.alert(person instanceof Person);
</script>
</head>
<body> </body>
</html>
obj05
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
function Person(name ,age) {
this.name = name;
this.age = age;
this.say = function () {
window.alert(this.name);
}
}
var person1 = new Person("wyp", 33);
var person2 = new Person("hg", 29);
window.alert(person1.say == person2.say);//false person1.say = function () {
window.alert(this.age);
}
person1.say(); person2.say(); </script>
</head>
<body> </body>
</html>
obj06
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
function Person() { }
Person.prototype.name = "wyp";
Person.prototype.age = 33;
Person.prototype.say = function () {
window.alert(this.name);
} var person1 = new Person();
var person2 = new Person();
person2.name = "hg";
person2.age = 29;
person1.say();
person2.say();
</script>
</head>
<body> </body>
</html>
obj07
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
function Person() { }
Person.prototype = {
constructor: Person,
name: "wyp",
age: 33,
works: ['gh', 'zyx'],
say: function () {
window.alert(this.name + ",[" + this.works + "]");
}
};
var person1 = new Person();
person1.name = "wyp";
person1.works.push("db");
person1.say(); var person2 = new Person();
person2.name = "sl";
person2.say(); </script>
</head>
<body> </body>
</html>
obj08
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
function Person(name, age, works) {
this.name = name;
this.age = age;
this.works = works;
if (!Person.prototype.say) {
Person.prototype.say = function () {
window.alert(this.name + ",[" + this.works + "]");
}
}
} var person1 = new Person("wyp", 33, ['hg', 'zyx']);
person1.works.push('db');
//person1.say = function () {
// window.alert(this.age);
//};
var person2 = new Person("gh", 29, ['hg', 'zyx']); person1.say();
person2.say(); window.alert(person1.say == person2.say);
</script>
</head>
<body> </body>
</html>
obj09
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
//function Person(name, age) {
// this.name = name;
// this.age = age;
// this.say = say;
//} function Person() { } Person.prototype.name = "name";
Person.prototype.age = 33;
Person.prototype.say = function () {
window.alert(this.name);
} function say() {
window.alert(this.name);
} var person1 = new Person("wyp", 33);
person1.name = "wyp";
person1.say = function () {
window.alert(this.age);
}
var person2 = new Person("gh", 29);
window.alert("person1.say == person2.say:" + (person1.say == person2.say)); window.alert("prototype.isPrototypeOf:" + Person.prototype.isPrototypeOf(person1)); window.alert("constructor:" + (person1.constructor == Person)); window.alert("name:" + person1.hasOwnProperty("name")); //delete person1.name;
//window.alert("name:" + person1.hasOwnProperty("name")); window.alert(" [in] " + ("name" in person1)); function isPrototypeProperty(obj,prop) {
return (!(obj.hasOwnProperty(prop)) && (prop in obj));
}
</script>
</head>
<body> </body>
</html>
obj10
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
( function (num) {
for (var i = 0; i < num; i++) { }
} )(20);
window.alert(i);
</script>
</head>
<body> </body>
</html>
close01
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script language="javascript" type="text/javascript">
function Parent() {
this.pv = "parent";
} Parent.prototype.showParent = function () {
window.alert(this.pv);
} function Child() {
this.cv = "child";
} Child.prototype = new Parent(); Child.prototype.showChild = function () {
window.alert(this.cv);
} var child= new Child();
//window.alert(child.pv);
child.showParent();
child.showChild(); </script>
</head>
<body> </body>
</html>
jicheng01
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
function Parent(name) {
this.color = ['red', 'blue'];
this.name = name;
this.say = say;
}
function say() {
window.alert(this.name);
} function Child(name, age) {
this.age = age;
Parent.call(this, name);
} var child1 = new Child("wyp", 33);
//child1.color.push("yellow");
//window.alert(child1.color);
//window.alert(child1.name);
//window.alert(child1.age);
var child2 = new Child("meinv", 23);
//window.alert(child1.name + "," + child1.age);
//window.alert(child2.name + "," + child2.age);
child1.say = function () {
window.alert(child1.age);
}
child1.say();
child2.say();
</script>
</head>
<body> </body>
</html>
jicheng02
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script language="javascript" type="text/javascript">
function Parent(name) {
this.name = name;
if(!Parent.prototype.say){
Parent.prototype.say = function () {
window.alert(this.name);
};
}
}
//Parent.prototype = {}; function Child(name, age) {
this.age = age;
Parent.call(this, name);
} Child.prototype = new Parent(); //重写父类say方法
//Child.prototype.say = function () {
// window.alert(this.name + "," + this.age);
//}; var child1 = new Child("wyp", 33);
child1.say(); </script>
</head>
<body> </body>
</html>
jicheng03
javascript中function和object的区别,以及javascript如何实现面向对象的编程思想.的更多相关文章
- JavaScript之Function 和 Object 的区别和联系
1.先看一个控制台的输出: instanceof 运算符字面意思是 左边是右边的一个实例吗? 但是这两条输出让人很困惑.Function 是 Object 的实例.Object 也是 Function ...
- javascript中Function与Object
1. 先来一段代码: console.log(Function); // function Function() { [native code] } console.log(Object); // f ...
- Javascript中Function,Object,Prototypes,__proto__等概念详解
http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...
- JavaScript中:表达式和语句的区别
JavaScript中:表达式和语句的区别 Javascript语言精粹:表达式是由运算符构成,并运算产生结果的语法结构.程序是由语句构成,语句则是由“:(分号)”分隔的句子或命令.如果在表达式后面加 ...
- Python中type与Object的区别
Python中type与Object的区别 在查看了Python的API后,总算明白了.现在总结如下: 先来看object的说明: Python中关于object的说明很少,甚至只有一句话: clas ...
- 转载 javascript中(function($){...})(jQuery)写法是什么意思
javascript中(function($){...})(jQuery)写法是什么意思 这里实际上是匿名函数function(arg){...}这就定义了一个匿名函数,参数为arg 而调用函数 ...
- javascript中 (function(){})();如何理解?
javascript中 (function(){})();如何理解? javascript中: (function(){})()是匿名函数,主要利用函数内的变量作用域,避免产生全局变量,影响整体页面环 ...
- JavaScript中Function函数与Object对象的关系
函数对象和其他内部对象的关系 除了函数对象,还有很多内部对象,比如:Object.Array.Date.RegExp.Math.Error.这些名称实际上表示一个 类型,可以通过new操作符返回一个对 ...
- JavaScript中Function原型及其prototype属性的简单应用
大家都知道在JavaScript中是没有类的概念的,但是却是有对象的概念的.有的人可能理解对象和类有些迷糊,这里简单的概括一下他们之间的区别: 类:抽象的概念,例如人,动物,汽车等都可以抽象成一个类 ...
随机推荐
- Servlet过滤器创建与配置
例1 创建一个过滤器,实现网站访问计数器的功能,并在web.xml文件的配置中,将网站访问量的初始值设置为5000. (1)创建名称为CountFilter的类,该类实现javax.servlet.F ...
- 第十一章 springboot + mongodb(简单查询)
1.mongodb在mac上的安装 下载mongodb,https://www.mongodb.org/ 解压缩到一个指定文件夹,如:/Users/enniu1/Desktop/zjg/mongodb ...
- Depth of field --Circle of confusion 推导
https://en.wikipedia.org/wiki/Circle_of_confusion https://developer.download.nvidia.com/books/HTML/g ...
- SQL Server基础知识三十三问 (7-14)
8. 一般什么时候使用update_statistics命令? 答: 这个命令基本上是在很多数据被处理过了之后才使用的. 如果大量的删除, 修改, 或这大量的数据插入已经发生了, 那么index就需 ...
- SQL基础(三):SQL命令
下面2个表用于实例演示: 1.SQL UNION 操作符 UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列.列 ...
- python - 增强的格式化字符串format函数
语法 它通过{}和:来代替%. “映射”示例 通过位置 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{},{}'.form ...
- C++设计模式实现--模板(Template)模式
一. 问题 在面向对象系统的分析与设计过程中常常会遇到这样一种情况:对于某一个业务逻辑(算法实现)在不同的对象中有不同的细节实现,可是逻辑(算法)的框架(或通用的应用算法)是同样的.Template提 ...
- SQL Server-已更新或删除的行值要么不能使该行成为唯一行,要么改变了多个行
在更新没有设置主键的表的时候出现下图中的问题: 问题原因: 这种问题大多是由于没有主键(PK)导致同一张表中存在若干条相同的数据 DBMS存储时,只为其存储一条数据,因为DBMS底层做了优化,以减少数 ...
- Python-__builtin__与__builtins__的区别与关系(超详细,经典)(转)
Python-__builtin__与__builtins__的区别与关系(超详细,经典) (2013-07-23 15:27:32) 转载▼ 分类: Python 在学习Python时,很多人会 ...
- HTML5游戏,五子棋
在线演示 本地下载 最近html5的游戏还真是不少,这种在线游戏既简单又有趣.收藏几个在午休时间娱乐一下.何乐而不为呢?喜欢研究的可以下载代码看看.超级推荐!