Mano
Mano

Reputation: 11

Object.define property doesn't support in SharePoint2010

I am using SharePoint 2010. While running my SharePoint webpart in Internet Explorer(I am using IE version 11)the below method should get the string value and display the value in alert message,instead it shows me an error "Object doesn't support this action". It stops at Object.defineProperty itself. This error occurs only in IE. The coding is as given below:

var obj = {
    name: 'abcd'
}
Object.defineProperty(obj, "Name", {
    get: function () { return this.name; }
});
alert(obj.Name);

Need to know why this error happens and how to fix it??

Upvotes: 1

Views: 206

Answers (1)

Thriggle
Thriggle

Reputation: 7059

Why is this happening?

This happens because SharePoint 2010 runs in compatibility mode in Internet Explorer in order to ensure all the add-ins work correctly. Compatibility mode emulates Internet Explorer 8, which had not yet introduced support for Object.defineProperty.

What can you do about it?

You could update the relevant masterpage to force the browser to render in Edge mode, but that can lead to unwelcome problems with the built-in SharePoint functionality.

The best route to take would be to check for the existence of Object.defineProperty before your code calls it and use a different approach when it's not available.

Here are a couple alternate approaches, but I'm sure there are other ways:

Using object constructors to define accessor/mutator functions

When you define your object as a constructor function, only variables that you define with the this keyword will be accessible as public members of objects created from that constructor. Any variables defined with the var keyword will be disposed of, unless they are referenced by any functions within the constructor function, in which case their references will be kept around by the closure, allowing them to act like private members.

var person = function(first,last){
    var firstname = first || "Bob"; // private member
    var lastname = last || "Smith"; // private member
    this.getName = getFullName; // public member
    function getFullName(){
        return firstname + " " + lastname;
    }
}
var john = new person("John","Deere");
alert(john.getName()); // "John Deere"
alert(john.firstname); // undefined

That has some pretty big disadvantages, the most obvious of which is that the function definitions that you slip into the constructor function get duplicated in memory for every object you create using the constructor.

Enter the prototype approach...

Using object prototypes to attach function definitions to your object class

A more memory-friendly approach is to only define variables in your constructor function, then attach any necessary member functions to the object prototype so that the functions are only defined once, no matter how many instances you create.

The downside of this approach is that functions defined outside the constructor function won't have access to the "private" variables defined within its scope, so you'll have to define them all with the this keyword.

var person = function(first,last){
    this.firstname = first || "Bob"; // public member
    this.lastname = last || "Smith"; // public member
}
// attach the getName() function to the prototype:
person.prototype.getName = function(){
    return this.firstname + " " + this.lastname;
}
var jane = new person("Jane","Doe");
alert(jane.getName()); // "Jane Doe"
alert(jane.firstname); // "Jane";

Upvotes: 1

Related Questions