js 高级函数 之示例
2014-04-07 11:13
225 查看
js 高级函数
作用域安全构造函数
function Person(name, age)
{
this.name = name;
this.age = age;
}
var person1 = Person("lin3615", 26);
//alert(window.name); // lin3615
alert(person1.name); // 出错,此时成了全局的了
========================
function Person(name, age)
{
this.name = name;
this.age = age;
}
var person1 = new Person("lin3615", 26);
//alert(window.name); // 为空
alert(person1.name); // lin3615
================
为了防止没有使用 new 时实例化,出现跨域,可用以下方法
function person(name, age)
{
if(this instanceof person)
{
this.name = name;
this.age = age;
}else
{
return new person(name,age);
}
}
var person1 = person("lin3615", 26);
//alert(window.name); // 为空
alert(person1.name); // lin3615
=========================
以下代码中 Polygon 构造函数是安全的,然后 Rectangle 构造函数则不是,一个 Rectangle 实例之后,这个实例应该通过
Polygon.call() 来继承 Polygon 的 sides 属性。但由于 Polygon
构造函数是作用域是安全的, this 对象并非 Polygon 的实例。所以
会创建并返回一个新的 Polygon 对象
function Polygon(sides)
{
if(this instanceof Polygon)
{
this.sides = sides;
this.getArea = function (){
return 0;
};
}else
{
return new Polygon(sides);
}
}
function Rectangle(width, height)
{
Polygon.call(this, 2);
this.width = width;
this.height = height;
this.getArea = function(){
return this.width *
this.height;
};
}
var rect = new Rectangle(5, 10);
alert(rect.sides); // undefined
如果结合原型链则可以解决这个问题
function Polygon(sides)
{
if(this instanceof Polygon)
{
this.sides = sides;
this.getArea = function (){
return 0;
};
}else
{
return new Polygon(sides);
}
}
function Rectangle(width, height)
{
Polygon.call(this, 2);
this.width = width;
this.height = height;
this.getArea = function(){
return this.width *
this.height;
};
}
Rectangle.prototype = new Polygon();
var rect = new Rectangle(5, 10);
alert(rect.sides); // 2
作用域安全构造函数
function Person(name, age)
{
this.name = name;
this.age = age;
}
var person1 = Person("lin3615", 26);
//alert(window.name); // lin3615
alert(person1.name); // 出错,此时成了全局的了
========================
function Person(name, age)
{
this.name = name;
this.age = age;
}
var person1 = new Person("lin3615", 26);
//alert(window.name); // 为空
alert(person1.name); // lin3615
================
为了防止没有使用 new 时实例化,出现跨域,可用以下方法
function person(name, age)
{
if(this instanceof person)
{
this.name = name;
this.age = age;
}else
{
return new person(name,age);
}
}
var person1 = person("lin3615", 26);
//alert(window.name); // 为空
alert(person1.name); // lin3615
=========================
以下代码中 Polygon 构造函数是安全的,然后 Rectangle 构造函数则不是,一个 Rectangle 实例之后,这个实例应该通过
Polygon.call() 来继承 Polygon 的 sides 属性。但由于 Polygon
构造函数是作用域是安全的, this 对象并非 Polygon 的实例。所以
会创建并返回一个新的 Polygon 对象
function Polygon(sides)
{
if(this instanceof Polygon)
{
this.sides = sides;
this.getArea = function (){
return 0;
};
}else
{
return new Polygon(sides);
}
}
function Rectangle(width, height)
{
Polygon.call(this, 2);
this.width = width;
this.height = height;
this.getArea = function(){
return this.width *
this.height;
};
}
var rect = new Rectangle(5, 10);
alert(rect.sides); // undefined
如果结合原型链则可以解决这个问题
function Polygon(sides)
{
if(this instanceof Polygon)
{
this.sides = sides;
this.getArea = function (){
return 0;
};
}else
{
return new Polygon(sides);
}
}
function Rectangle(width, height)
{
Polygon.call(this, 2);
this.width = width;
this.height = height;
this.getArea = function(){
return this.width *
this.height;
};
}
Rectangle.prototype = new Polygon();
var rect = new Rectangle(5, 10);
alert(rect.sides); // 2
相关文章推荐
- js高级编号笔记[新]-函数
- JS中实现简单Formatter函数示例代码
- js高级——变量和函数提升
- 《js高级程序设计》--函数
- 从零学JS高级程序设计一:定义函数的方式,函数表达式和函数声明的区别
- JS中实现简单Formatter函数示例代码
- js生成随机数之random函数随机示例
- JS的document.all函数使用示例
- js只执行1次的函数示例
- js高级程序设计(第三版)总结--函数的详解及return的用法
- js编码、解码函数介绍及其使用示例
- js高级——引用类型和值类型在函数中的使用
- js 时间函数应用加、减、比较、格式转换的示例代码
- JS高级函数--------filter、sort
- angular.js和vue.js中实现函数去抖示例(debounce)
- JS高级程序设计笔记(六)- 函数
- js中函数传递中文参数示例
- Js和Jquery中小数据点保留2位或几位的函数!(示例)
- JS高级技巧之函数节流
- 纯js封装的ajax功能函数与用法示例