js 个别操作符

in 操作符:

    The in operator expects a left-side operand that is or can be converted to a string(或可以被转换为字符串). It expects a right-side operand that is an object (or array)(对象或数组). It evaluates to TRue if the left-side value is the name of a property of the right-side object. For example:

(翻译:in 操作符要求 其左边的操作数是字符串或者可以转换为字符串,右边的操作数是个对象(或是数组)。

如果左边的值是右边对象的一个属性的名字 返回true


)
var point = { x:1, y:1 };        // Define an object
var has_x_coord = "x" in point;  // Evaluates to true
var has_y_coord = "y" in point;  // Evaluates to true
var has_z_coord = "z" in point;  // Evaluates to false; not a 3-D point
var ts = "toString" in point;    // Inherited property; evaluates to true 被继承的属性。


typeof运算符


The typeof operator evaluates to "number", "string", or "boolean" if its operand is a number, string, or boolean value. It evaluates to "object" for objects, arrays, and (surprisingly) null. It evaluates to "function" for function operands and to "undefined" if the operand is undefined.

对 "number", "string", or "boolean" 的包装类,返回的也是Object 类型,


   举例:

typeof i;
(typeof value == "string") ? "'" + value + "'" : value;

也可以这样来写

typeof(i);


typeof对所有的对象和数组返回的都是Object类型,所以它只有区分原始类型和引用类型有效。要区别对象类型则要用 instanceof 和construtor属性才行。


instanceof 运算符(The instanceof Operator)


The instanceof operator expects a left-side operand that is an object and a right-side operand that is the name of a class of objects.

(instanceof 操作符要求左边的操作数是一个对象,右边的操作数是对象的类的名字)

in JavaScript, classes of objects are defined by the constructor function that initializes them(翻译:对象的类是由初始化他们的构造函数定义的)

Thus, the right-side operand of instanceof should be the name of a constructor function.

(因此,instanceof 右边的操作应当是构造函数的名字)

Note that all objects are instances of Object

(注意,所有的对象都是Object的实例)





var d = new Date( );  // Create a new object with the Date( ) constructor
d instanceof Date;   // Evaluates to true; d was created with Date( )
d instanceof Object; // Evaluates to true; all objects are instances of Object
d instanceof Number; // Evaluates to false; d is not a Number object
var a = [1, 2, 3];   // Create an array with array literal syntax
a instanceof Array;  // Evaluates to true; a is an array
a instanceof Object; // Evaluates to true; all arrays are objects
a instanceof RegExp; // Evaluates to false; arrays are not regular expressions


  

If the left-side operand of instanceof is not an object, or if the right-side operand is an object that is not a constructor function, instanceof returns false. However, it returns a runtime error if the right-side operand is not an object at all
(如果instanceof 左边的操作数不是一个对象,或者右边的操作数不是一个对象的构造函数,instanceof 返回false。

不管怎么样,它会返回一个运行时错误,如果右边的操作数根本不是一个对象)

void 运算符(The void Operator)



     The purpose of this operator is an unusual one: it discards its operand value and returns undefined.

(这个操作符的目的和寻常的不同,它会丢弃操作数的值 并且返回undefined
The most common use for this operator is in a client-side javascript: URL, where it allows you to evaluate an expression for its side effects without the browser displaying the value of the evaluated expression

(这个操作符经常应用于客户端的javascript:Url。。。。)
Open New Window

Another use for void is to purposely generate the undefined value

 

[b]new 运算符(The Object-Creation Operator (new))


The new operator creates a new object and invokes a constructor function to initialize it

new constructor(arguments)[/b]

constructor must be an expression that evaluates to a constructor function.

o = new Object;    // Optional parentheses omitted here
d = new Date( ); 

 

The new operator first creates a new object with no properties defined. Next, it invokes the specified constructor function, passing the specified arguments and also passing the newly created object as the value of the this keyword. The constructor function can then use the this keyword to initialize the new object in any way desired

(

1.new 操作符首先会创建一个新的没有定义任何属性的对象。

2.然后 它会调用一个指定的构造函数,

3.传递指定的参数,

4.传递新创建的对象作为this关键字的值。

这个构造函数可以用this关键字以你任何想要的方式初始化新的对象

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值