[Javascript Note] global Object and Object

Source: Internet
Author: User

Global Objects in javascript are a parent-level structure. All other objects, variables, and functions belong to global objects. Another type is the Object. Other Objects in javascript are inherited from objects, including built-in objects.

A Global Object

1. Features of Global Objects

  • A global object is only an object, not a class. Neither a constructor nor a new global object can be instantiated.
  • The global object is not an attribute of any object, so it has no name.
  • A global object is a predefined object and serves as a placeholder for global functions and global attributes of JavaScript.
  • In the top-level JavaScript code, you can use the keyword "this" to reference a global object.
  • The global object is the header of the scope chain, meaningAll variables declared in the top-level JavaScript will become attributes of the global object.
  • Generally, you do not need to use this to reference global objects, because all non-restrictive variables and function names are queried as attributes of global objects.
  • By using global objects, you can access all other predefined objects, functions, and attributes.
  • The predefined attributes of global objects are enumerative.You can use a for/in loop to list all implicitly or explicitly declared global variables.

The global object is the context of the top layer, and all other objects belong to the global object. In web development, global objects provide the top-level context for the scope, and provide a shared platform for some high-level browser functions that web development depends on. In addition, global objects include the following:

  • All built-in objects in javascript
  • Common built-in attributes in javascript
  • Some built-in functions in javascript
  • Some specific browser objects, such as document and window objects

Global Objects also provide very useful functions for scopes. When a variable is referenced and the variable is not found in the current scope, the interpreter moves up along the scope chain and searches for the variable along the scope chain until the global object is found. If the variable cannot be found, the interpreter considers that the variable does not exist and triggers an exception. In the global scope, you can use the keyword "this" to access the global object.

2. Global Objects in the browser

Global Objects have special purposes in browsers. In addition to saving all common functions in javascript and serving as a global context, it also contains a large number of browser-specific objects. For example, window and document objects. The window object is very interesting. The window attribute is a self-referenced member. You can use window. property to access variables, or simply use property to access variables. This is because javascript has a global object and a window object that is essentially the same as it.

In the browser, each window, iFrame, and option card have their own unique global objects. In some cases, the DOM can communicate between these scopes, but in general these scopes are mutually independent and isolated.

Instance 1

Obtain Global Objects

1   <script type="text/javascript">
2 function getGlobalObject(){
3 return (function(){
4 return this;
5 }).call(null);
6 }
7 var globalobj=getGlobalObject();
8 alert(globalobj);
9 </script>

Note: Some global attributes can be overwritten, such as undefined.

 

2. Object

The Object is the base class of other objects in the javascript language. Through the Object, developers can instantiate user-defined objects at runtime.

Attribute list

  • Object. constructor
  • Object. prototype
  • Obect. _ parent _
  • Object. _ proto _

Method list

  • Object. eval ()
  • Object. hasOwnProperty ()
  • Object. isPrototypeOf ()
  • Object. propertyIsEnumerable ()
  • Object. toLocaleString ()
  • Object. toSource ()
  • Object. toString ()
  • Object. unwatch ()
  • Object. valueOf ()
  • Object. watch ()
  • Object. _ defineGetter_0
  • Object. _ defineSetter_0
  • Object. _ lookupGetter_0
  • Object. _ lookupSetter_0
  • Object. _ noSuchMethod_0

Object:

  1. When developing javascript in a fully controllable environment, using prototype inheritance to extend the javascript base class is a safe practice.
  2. Difference between the valueOf () method and toString () method: the valueOf () method returns the basic value of an object, while the toString () method is used to return the most useful text value of an object.

Instance 2

Merged object

<Script type = "text/javascript"> Object. prototype. merge = function (obj) {// make sure that the processed object is a valid object if (typeof this = "object" & obj & typeof obj = "object ") {for (var arg in obj) {if (typeof obj [arg] = "object "&&! Obj [arg]. length) {if (! This [arg]) {this [arg] ={};} this [arg]. merge (obj [arg]);} else {this [arg] = this [arg] | obj [arg] ;}}} var person = {name: "unkonwn", age: 0, height: "no", weight: "no", occupation: "no", children: {count: 0, names: []} var kycool = {name: "keyool", age: 25, occupation: "code man"} kycool. merge (person); for (var m in kycool) {document. write (m + ":" + kycool [m] + "<br/>") ;}</script>

Running result:

Name: keyoolage: 25 occupation: code manmerge: function (obj) {// ensure that the processed object is a valid object if (typeof this = "object" & obj & typeof obj = "object "){
     for(var arg in obj){ if(typeof obj[arg]=="object" && !obj[arg].length){ if(!this[arg]){ this[arg]={}; } 
      this[arg].merge(obj[arg]); }else{ this[arg]=this[arg]||obj[arg]; } } } }height:noweight:nochildren:[object Object]

Override isType Class Functions

// ================================== IsType series ====================== =====// Object. isArray = function (obj) {// check whether the parameter is an object and its constructor is the array constructor return (typeof obj = "object" & obj. constructor = Array);} Object. isDate = function (obj) {// check whether the parameter is an object and its constructor is the return (typeof obj = "object" & obj. constructor = Date);} Object. isNumberObject = function (obj) {// check whether the parameter is an object and its constructor is the constructor of the Number object. return (typeof obj = "object" & obj. constructor = Num Ber);} Object. isStringObject = function (obj) {// check whether the parameter is an object and its constructor is the String object constructor return (typeof obj = "object" & obj. constructor = String);} Object. isBooleanObject = function (obj) {// check whether the parameter is an object and its constructor is the return (typeof obj = "object" & obj. constructor = Boolean);} Object. isFunction = function (obj) {// check whether the parameter is a function Object return (typeof obj = "function");} Object. isObject = function (obj) {// check whether the parameter is an object return (typeof ob J = "object "&&!! Obj) | (typeof obj = "function");} Object. isRegex = function (obj) {// check whether the parameter is a RegExp object return (typeof obj = "object" & obj. constructor = RegExp);} Object. isString = function (obj) {// check whether the parameter is a string return (typeof obj = "string");} Object. isNumber = function (obj) {// check whether the parameter is a value return (typeof obj = "number" & isFinite (obj);} Object. isBoolean = function (obj) {// check whether the parameter is a boolean return (typeof obj = "boolean");} Object. isUndefined = function (Obj) {// check whether the parameter is undefinedreturn (typeof obj = "undefined");} Object. isNull = function (obj) {// check whether the parameter is nullreturn (typeof obj = "object "&&! Obj );}

 

 

Related Article

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.