js 原型
1: function Person (name,age) {
2: this.name = name;
3: this.age = age;
4: }
5:
6: Person.prototype = {
7: constructor : Person, // 原型{}这种格式得重新定义指向 要不然默认是Object
8: getInfo: function() {
9: return 'A ' + this.name + ' ' + this.age + '.';
10: }
11: };
12:
13: var p = new Person('Lily',18);
14:
15: /**
16: * Person的prototype属性指向Person的prototype对象
17: * Person的实例p的__proto__属性指向Preson的prototype对象
18: * Preson的prototype对象的constructor属性指向Person
19: */
20: console.log(p.constructor); // Person
21: console.log(p.__proto__ == Person.prototype); // true
22: console.log(p.__proto__.constructor == Person); // true p.__proto__ 指向Person的prototype对象 Person的prototype对象constructor属性指向Person构造器
23:
24: //Object.create EcmaScript5 以上有
25: var Person2 = Object.create(p); //复制对象
26: console.log(Person2.name);// Lily
27: Person2.age = 20;
28: console.log(Person2.age);// 20
29: console.log(p.age);// 20
30:
31: var o1 = Object.create({x:1, y:2});
32: var o2 = Object.create(Object.prototype);
33: console.log(o2);
34: console.log(new Object());
1: function Person (name,age) {
2: this.name = name;
3: this.age = age;
4: }
5:
6: Person.prototype = {
7: constructor : Person, // 原型{}这种格式得重新定义指向 要不然默认是Object
8: getInfo: function() {
9: return 'A ' + this.name + ' ' + this.age + '.';
10: }
11: };
12:
13: var p = new Person('Lily',18);
14:
15: /**
16: * Person的prototype属性指向Person的prototype对象
17: * Person的实例p的__proto__属性指向Preson的prototype对象
18: * Preson的prototype对象的constructor属性指向Person
19: */
20: console.log(p.constructor); // Person
21: console.log(p.__proto__ == Person.prototype); // true
22: console.log(p.__proto__.constructor == Person); // true p.__proto__ 指向Person的prototype对象 Person的prototype对象constructor属性指向Person构造器
23:
24: //Object.create EcmaScript5 以上有
25: var Person2 = Object.create(p); //复制对象
26: console.log(Person2.name);// Lily
27: Person2.age = 20;
28: console.log(Person2.age);// 20
29: console.log(p.age);// 20
30:
31: var o1 = Object.create({x:1, y:2});
32: var o2 = Object.create(Object.prototype);
33: console.log(o2);
34: console.log(new Object());
js 原型的更多相关文章
- JS原型链
JS作为发展了多年了对象语言,支持继承,和完全面向对象语言不同的是,JS依赖原型链来实现对象的继承. 首先JS的对象分两大类,函数对象和普通对象,每个对象均内置__proto__属性,在不人为赋值__ ...
- 深入分析JS原型链以及为什么不能在原型链上使用对象
在刚刚接触JS原型链的时候都会接触到一个熟悉的名词:prototype:如果你曾经深入过prototype,你会接触到另一个名词:__proto__(注意:两边各有两条下划线,不是一条).以下将会围绕 ...
- 【09-23】js原型继承学习笔记
js原型继承学习笔记 function funcA(){ this.a="prototype a"; } var b=new funcA(); b.a="object a ...
- js原型
1.js基本类型和对象类型 js的简单类型包括数字(其中NaN为数字类型).字符串(类似'A'为字符,js没字符类型).布尔值.null值和undefined值.其他所有的值都是对象.数字.字符串和布 ...
- js原型链与继承(初体验)
js原型链与继承是js中的重点,所以我们通过以下三个例子来进行详细的讲解. 首先定义一个对象obj,该对象的原型为obj._proto_,我们可以用ES5中的getPrototypeOf这一方法来查询 ...
- JS 原型链图形详解
JS原型链 这篇文章是「深入ECMA-262-3」系列的一个概览和摘要.每个部分都包含了对应章节的链接,所以你可以阅读它们以便对其有更深的理解. 对象 ECMAScript做为一个高度抽象的面向对象语 ...
- js原型解析
我们都知道javascript因为具有了继承以及变量等等一系列的特性之后才被人们认为具有一门编程语言的资格,在后续的不断发展中,js在原生的基础上扩展了基于jquery等等的库,甚至衍生了像node. ...
- 深入理解JS原型链与继承
我 觉得阅读精彩的文章是提升自己最快的方法,而且我发现人在不同阶段看待同样的东西都会有不同的收获,有一天你看到一本好书或者好的文章,请记得收藏起来, 隔断时间再去看看,我想应该会有很大的收获.其实今天 ...
- 学习zepto.js(原型方法)
学习zepto.js(原型方法)[1] 转载 新的一周,新的开始,今天来学习一下zepto里边的原型方法,就是通过$.进行调用的方法,也是可以通过$.fn进行扩展的方法: $.camelCase(): ...
- js原型链部分详细使用说明案例
1. 'index.html'文件 ```html <!DOCTYPE html> <html lang="en"> <head> <me ...
随机推荐
- php性能效率优化
[size=5][color=Red]php性能效率优化[/color][/size] 最近在公司一边自学一边写PHP程序,由于公司对程序的运行效率要求很高,而自己又是个新手,一开始就注意程序的效率很 ...
- mysql中自动更新时间CURRENT_TIMESTAMP
timestamp的两个属性:CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP http://blog.163.com/qiongling007@126/b ...
- [Google Code Jam (Round 1A 2008) ] A. Minimum Scalar Product
Problem A. Minimum Scalar Product This contest is open for practice. You can try every problem as ...
- final关键字的作用
final 可以修饰类.方法.变量. (1):final修饰的变量是一个常量,只能被赋值一次. 常量一般使用final声明,如: public static final String CHINA = ...
- 我的Android4.3新书即将上市,谢谢大家的支持
首先感谢清华大学.电子工业.机械工业.人民邮电等各大出版社对本书的肯定.我想说中国的IT业如果没有你们的辛勤工作,是不会发展得这么快的.经过再三权衡,本书将选择人民邮电出版社于近几个月在全国出版发行. ...
- 【转】Device Tree(三):代码分析
原文网址:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html 一.前言 Device Tree总共有三篇,分别是: 1.为何要引入De ...
- 【转】android移植之request_suspend_state: wakeup & init: untracked pid xx exited问题的解决
原文网址:http://blog.csdn.net/dragon101788/article/details/8187880 Bluetooth: HIDP (Human Interface Emul ...
- C++关于strcpy等函数的安全版本
如下程序: #include <iostream> using namespace std; int main() { ]; strcpy(ch1,"); } 在VS2012上面 ...
- Javascript构造函数学习
我们经常会用JS的构造函数实现Java语言中的继承,今天整理一下构造函数的相关属性及说明. 下面定义一个构造函数: function Person(name, sex, age) { this.nam ...
- java匿名内部类,多态,接口练习
1多态以及接口调用方法: public class Interface { public static void main(String[] args) { Al x = new Al(); jian ...