js实例化的对象,函数和原型的相关
来源:互联网 发布:c语言的软件是什么 编辑:程序博客网 时间:2024/06/06 04:42
javascript是一个单线程的语言,但是可以通过代码调度在特定的时间运行。
对于js而言,每个实例化的对象都有以下的方法和属性(也就是说共有的,既然共有那麽就是在原型上的了):
(1):constructor,constructor被用来创建对象,比如 var o = new Object();那么constructor 就是 Object()函数。
(2):hasOwnProperty(propertyname),这表明如果一个实例化的对象(不是原型)存在给定的属性;注意的是属性名必须是字符串的形式。
(3):isPrototypeOf(object),判定一个对象是否是另一个对象的原型。
alert(Person.prototype.isPrototypeOf(person1)); //true
alert(Person.prototype.isPrototypeOf(person2)); //true
(4):propertyIsEnumerable(propertyname), 一个给定的属性可以用for-in语句枚举;同hasOwnProperty()一样,属性名必须是字符串。
(5):toString():返回对象的字符串形式
(6):valueOf():返回一个等于对象的字符串,布尔,字符串的值;这个经常返回和toString()一样的值。
对于js函数内部变量而言,函数执行结束,内部变量也就被销毁了。
对于判定变量的类型,基值类型变量用typeof ,引用类型变量用instance of最好了。
对于ECMAScript而言,函数(首字母大写)就是构造函数,(一般首字母大写,函数返回类型:Object.prototype.toString.call(a)返回[object Function])。参考一个例子:
function Person(name, age, job){this.name = name;this.age = age;this.job = job;this.sayName = new function(){ return this.name }; //logical equivalent } var person1 = new Person('Nicholas', 29, 'Software Engineer');var person2 = new Person('Greg', 27, 'Doctor');alert(person1.sayName)alert(person1.sayName == person2.sayName); //false 不是原型上的,是对象的实例化
2:(function(){alert('ddd')})(); //ddd
3:var x=new function(){ return new String('ddd')};
alert(x); //ddd
4:var yx01 = new function() {return "ddd"};
alert(yx01);
将返回显示[object object] ,此时该代码等价于:
function 匿名类(){
return "ddd";
}
var yx01 = new 匿名类();
alert(yx01);
提示:注意 new function 和 new Function的区别。
看一个继承的例子:
function A(){ this.name='ikol'; this.hello='hello world';}A.prototype.getName=function(){ return this.name;};A.prototype.sayHello=function(){ return this.hello;}function B(){ A.call(this);};B.prototype=A.prototype;var b=new B();console.log(b.getName());console.log(b.sayHello());
下面的看一个原型的例子:
function SuperType(){ this.property = true;}SuperType.prototype.getSuperValue = function(){ return this.property;};function SubType(){ this.subproperty = false;}//继承自SuperTypeSubType.prototype = new SuperType();//试着增加一些新的方法 - 这将清空(覆盖)上面的一行SubType.prototype = { getSubValue : function (){ return this.subproperty; }, someOtherMethod : function (){ return false; }};var instance = new SubType();alert(instance.getSuperValue()); //错误!
再看一个原型的例子代码如下,图形描述附最后:
function SuperType(name){ this.name = name; this.colors = [“red”, “blue”, “green”];}SuperType.prototype.sayName = function(){ alert(this.name);};function SubType(name, age){ //inherit properties SuperType.call(this, name); this.age = age;}//inherit methodsSubType.prototype = new SuperType();SubType.prototype.sayAge = function(){ alert(this.age);};var instance1 = new SubType(“Nicholas”, 29);instance1.colors.push(“black”);alert(instance1.colors); //”red,blue,green,black”instance1.sayName(); //”Nicholas”;instance1.sayAge(); //29var instance2 = new SubType(“Greg”, 27);alert(instance2.colors); //”red,blue,green”instance2.sayName(); //”Greg”;instance2.sayAge(); //27
- js实例化的对象,函数和原型的相关
- js入门实例の构造函数/方法的定义和调用/原型对象
- js入门实例の构造函数/方法的定义和调用/原型对象
- js的原型和对象
- 一句话总结JS构造函数、原型和实例的关系
- 关于js中,原型对象,原型链,构造函数,实例之间关系的理解与区别
- JS面向对象-原型对象,实例对象,构造函数的关系
- 关于js的构造函数和原型对象
- 简单的原型,原型对象和实例对象先后顺序
- 【JavaScript】JavaScript原型链之原型对象、实例和构造函数三者之间的关系
- 图解js原型(原型,对象,函数之间的关系)
- js的原型链和对象理解
- 对js对象和原型的理解
- 谈谈JS的核心技术:原型对象和原型链
- 谈谈JS的核心技术:原型对象和原型链
- 秒懂JS对象、构造器函数和原型对象之间的关系
- js关于原型构造函数和原型链的理解
- js 创建对象的两种主要方法 构造函数 和 原型+构造函数组合
- fseek
- 关于java中Axis2客户端调用C#webservice服务端包含Cookie问题
- ftell
- C++11 并发指南一(C++11 多线程初探)
- APP适配IOS8,iPhone6和Plus截图简要说明,有iP6截图下载供研究(原创)
- js实例化的对象,函数和原型的相关
- web 开发笔记“CSS hack大全之特殊符号的应用解决浏览器兼容性问题”
- fwrite
- DBus如何使用原始DBus库传送和接收数据
- 下载频道2013上半年超人气精华资源汇总
- getc
- getchar
- Android Framework------之PowerManagerService的功能
- XSD检验报文是否符合设计