JavaScript Object properties Check, add, delete, access operation instances _javascript tips

Source: Internet
Author: User
Tags access properties hasownproperty

Check Properties

var mouse = {
 "name": "Betta",
 "age": 3,
 "varieties": "Milaoshu"
}
 
Mouse.hasownproperty ("name"); True
mouse.hasownproperty ("sex");//false

Add properties

Defines an object dog, then assigns a variety of attributes, then gives the color attribute, and finally traverses all attributes and values

var dog={
 name: "Mango",
 type: "King of the Will",
 eat:function () {
  alert ("Eat");
 }
 object.prototype.color= "White";
 var name;
 for (name in dog) {
  document.write (name+ "" +dog[name]+ "<br>")
 }

The effect is as follows

Name Mango
type will King
eat function () {alert ("Eat");}
Color White

Delete attribute

var cat = {
  "name": "Tom",
  "sex": "Man",
  "color": "Yellow"
}
delete cat.name;
Cat.sex = undefined;
Cat.color = null;
Alert ("Name property exists:" + cat.hasownproperty ("name")); False
alert ("Sex property exists:" + cat.hasownproperty ("sex"));//true
alert ("Color property exists:" + cat.hasownproperty ( "Color")); True

Access Properties

var cat = {
  "name": "Tom",
  "sex": "Man",
  "color": "Yellow"
}
var name1 = cat.name;// Accessing object Properties
var name2 = cat["name" via the dot operator;//access to object properties through bracket operators

There are two ways to create objects

var obj = new Object ();
Obj.name = "Mangguo";
Obj.age =;

var obj = {
  name: "Mangguo",//name is the property name, "Mangguo" is the value
  age:25
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.